Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Scope.h" |
| 18 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 19 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 20 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // C99 6.7: Declarations. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | /// ParseTypeName |
| 31 | /// type-name: [C99 6.7.6] |
| 32 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 33 | /// |
| 34 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 35 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 36 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 37 | AccessSpecifier AS, |
| 38 | Decl **OwnedType) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 40 | DeclSpec DS(AttrFactory); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 41 | ParseSpecifierQualifierList(DS, AS); |
| 42 | if (OwnedType) |
| 43 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 44 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 46 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 48 | if (Range) |
| 49 | *Range = DeclaratorInfo.getSourceRange(); |
| 50 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 51 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 52 | return true; |
| 53 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 54 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 57 | |
| 58 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 59 | /// require late parsing. |
| 60 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 61 | return llvm::StringSwitch<bool>(II.getName()) |
| 62 | #include "clang/Parse/AttrLateParsed.inc" |
| 63 | .Default(false); |
| 64 | } |
| 65 | |
| 66 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 67 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | /// |
| 69 | /// [GNU] attributes: |
| 70 | /// attribute |
| 71 | /// attributes attribute |
| 72 | /// |
| 73 | /// [GNU] attribute: |
| 74 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 75 | /// |
| 76 | /// [GNU] attribute-list: |
| 77 | /// attrib |
| 78 | /// attribute_list ',' attrib |
| 79 | /// |
| 80 | /// [GNU] attrib: |
| 81 | /// empty |
| 82 | /// attrib-name |
| 83 | /// attrib-name '(' identifier ')' |
| 84 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 85 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 86 | /// |
| 87 | /// [GNU] attrib-name: |
| 88 | /// identifier |
| 89 | /// typespec |
| 90 | /// typequal |
| 91 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 95 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | /// start with that identifier; otherwise they are an expression list." |
| 97 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 98 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 99 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 101 | /// any attributes that don't work (based on my limited testing). Most |
| 102 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 103 | /// a pressing need to implement the 2 token lookahead. |
| 104 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 105 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 106 | SourceLocation *endLoc, |
| 107 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 108 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 110 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | ConsumeToken(); |
| 112 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 113 | "attribute")) { |
| 114 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 115 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | } |
| 117 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 118 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 119 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 122 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 123 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 126 | ConsumeToken(); |
| 127 | continue; |
| 128 | } |
| 129 | // we have an identifier or declaration specifier (const, int, etc.) |
| 130 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 131 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 133 | if (Tok.is(tok::l_paren)) { |
| 134 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 135 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 136 | LateParsedAttribute *LA = |
| 137 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 138 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 139 | |
| 140 | // Attributes in a class are parsed at the end of the class, along |
| 141 | // with other late-parsed declarations. |
| 142 | if (!ClassStack.empty()) |
| 143 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 145 | // consume everything up to and including the matching right parens |
| 146 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 148 | Token Eof; |
| 149 | Eof.startToken(); |
| 150 | Eof.setLocation(Tok.getLocation()); |
| 151 | LA->Toks.push_back(Eof); |
| 152 | } else { |
| 153 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | } |
| 155 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 156 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 157 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 162 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 163 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 164 | SkipUntil(tok::r_paren, false); |
| 165 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 166 | if (endLoc) |
| 167 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 171 | |
| 172 | /// Parse the arguments to a parameterized GNU attribute |
| 173 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 174 | SourceLocation AttrNameLoc, |
| 175 | ParsedAttributes &Attrs, |
| 176 | SourceLocation *EndLoc) { |
| 177 | |
| 178 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 179 | |
| 180 | // Availability attributes have their own grammar. |
| 181 | if (AttrName->isStr("availability")) { |
| 182 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 183 | return; |
| 184 | } |
| 185 | // Thread safety attributes fit into the FIXME case above, so we |
| 186 | // just parse the arguments as a list of expressions |
| 187 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 188 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | ConsumeParen(); // ignore the left paren loc for now |
| 193 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 194 | IdentifierInfo *ParmName = 0; |
| 195 | SourceLocation ParmLoc; |
| 196 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 197 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 198 | switch (Tok.getKind()) { |
| 199 | case tok::kw_char: |
| 200 | case tok::kw_wchar_t: |
| 201 | case tok::kw_char16_t: |
| 202 | case tok::kw_char32_t: |
| 203 | case tok::kw_bool: |
| 204 | case tok::kw_short: |
| 205 | case tok::kw_int: |
| 206 | case tok::kw_long: |
| 207 | case tok::kw___int64: |
| 208 | case tok::kw_signed: |
| 209 | case tok::kw_unsigned: |
| 210 | case tok::kw_float: |
| 211 | case tok::kw_double: |
| 212 | case tok::kw_void: |
| 213 | case tok::kw_typeof: |
| 214 | // __attribute__(( vec_type_hint(char) )) |
| 215 | // FIXME: Don't just discard the builtin type token. |
| 216 | ConsumeToken(); |
| 217 | BuiltinType = true; |
| 218 | break; |
| 219 | |
| 220 | case tok::identifier: |
| 221 | ParmName = Tok.getIdentifierInfo(); |
| 222 | ParmLoc = ConsumeToken(); |
| 223 | break; |
| 224 | |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | ExprVector ArgExprs(Actions); |
| 230 | |
| 231 | if (!BuiltinType && |
| 232 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 233 | // Eat the comma. |
| 234 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 235 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 236 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 237 | // Parse the non-empty comma-separated list of expressions. |
| 238 | while (1) { |
| 239 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 240 | if (ArgExpr.isInvalid()) { |
| 241 | SkipUntil(tok::r_paren); |
| 242 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 243 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 244 | ArgExprs.push_back(ArgExpr.release()); |
| 245 | if (Tok.isNot(tok::comma)) |
| 246 | break; |
| 247 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 248 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 249 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 250 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 251 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 252 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 253 | while (Tok.is(tok::identifier)) { |
| 254 | ConsumeToken(); |
| 255 | if (Tok.is(tok::greater)) |
| 256 | break; |
| 257 | if (Tok.is(tok::comma)) { |
| 258 | ConsumeToken(); |
| 259 | continue; |
| 260 | } |
| 261 | } |
| 262 | if (Tok.isNot(tok::greater)) |
| 263 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 264 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 265 | } |
| 266 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 267 | |
| 268 | SourceLocation RParen = Tok.getLocation(); |
| 269 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 270 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 271 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 272 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Michael Han | e53ac8a | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 273 | if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 274 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 279 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 280 | /// |
| 281 | /// [MS] decl-specifier: |
| 282 | /// __declspec ( extended-decl-modifier-seq ) |
| 283 | /// |
| 284 | /// [MS] extended-decl-modifier-seq: |
| 285 | /// extended-decl-modifier[opt] |
| 286 | /// extended-decl-modifier extended-decl-modifier-seq |
| 287 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 288 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 289 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 290 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 291 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 292 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 293 | "declspec")) { |
| 294 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 295 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 296 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 297 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 298 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 299 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 300 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 301 | |
| 302 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 303 | // Just skip everything inside property(). |
| 304 | if (AttrName->getName() == "property") { |
| 305 | ConsumeParen(); |
| 306 | SkipUntil(tok::r_paren); |
| 307 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 308 | if (Tok.is(tok::l_paren)) { |
| 309 | ConsumeParen(); |
| 310 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 311 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 312 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 313 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 314 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 315 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 316 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 317 | } |
| 318 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 319 | SkipUntil(tok::r_paren, false); |
| 320 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 321 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 322 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 326 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 327 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 328 | } |
| 329 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 330 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 331 | // Treat these like attributes |
| 332 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 333 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 334 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 335 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 336 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 337 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 338 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 339 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 340 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 341 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 342 | // FIXME: Support these properly! |
| 343 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 344 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 345 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 346 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 347 | } |
| 348 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 349 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 350 | // Treat these like attributes |
| 351 | while (Tok.is(tok::kw___pascal)) { |
| 352 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 353 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 354 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 355 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 356 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 359 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 360 | // Treat these like attributes |
| 361 | while (Tok.is(tok::kw___kernel)) { |
| 362 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 363 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 364 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 365 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 369 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 370 | SourceLocation Loc = Tok.getLocation(); |
| 371 | switch(Tok.getKind()) { |
| 372 | // OpenCL qualifiers: |
| 373 | case tok::kw___private: |
| 374 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 375 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 376 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 377 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 381 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 382 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 383 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 384 | break; |
| 385 | |
| 386 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 387 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 388 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 389 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 390 | break; |
| 391 | |
| 392 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 393 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 394 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 395 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 399 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 400 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 401 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 402 | break; |
| 403 | |
| 404 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 405 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 406 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 407 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 411 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 412 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 413 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 414 | break; |
| 415 | default: break; |
| 416 | } |
| 417 | } |
| 418 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 419 | /// \brief Parse a version number. |
| 420 | /// |
| 421 | /// version: |
| 422 | /// simple-integer |
| 423 | /// simple-integer ',' simple-integer |
| 424 | /// simple-integer ',' simple-integer ',' simple-integer |
| 425 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 426 | Range = Tok.getLocation(); |
| 427 | |
| 428 | if (!Tok.is(tok::numeric_constant)) { |
| 429 | Diag(Tok, diag::err_expected_version); |
| 430 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 431 | return VersionTuple(); |
| 432 | } |
| 433 | |
| 434 | // Parse the major (and possibly minor and subminor) versions, which |
| 435 | // are stored in the numeric constant. We utilize a quirk of the |
| 436 | // lexer, which is that it handles something like 1.2.3 as a single |
| 437 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 438 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 439 | Buffer.resize(Tok.getLength()+1); |
| 440 | const char *ThisTokBegin = &Buffer[0]; |
| 441 | |
| 442 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 443 | bool Invalid = false; |
| 444 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 445 | if (Invalid) |
| 446 | return VersionTuple(); |
| 447 | |
| 448 | // Parse the major version. |
| 449 | unsigned AfterMajor = 0; |
| 450 | unsigned Major = 0; |
| 451 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 452 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 453 | ++AfterMajor; |
| 454 | } |
| 455 | |
| 456 | if (AfterMajor == 0) { |
| 457 | Diag(Tok, diag::err_expected_version); |
| 458 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 459 | return VersionTuple(); |
| 460 | } |
| 461 | |
| 462 | if (AfterMajor == ActualLength) { |
| 463 | ConsumeToken(); |
| 464 | |
| 465 | // We only had a single version component. |
| 466 | if (Major == 0) { |
| 467 | Diag(Tok, diag::err_zero_version); |
| 468 | return VersionTuple(); |
| 469 | } |
| 470 | |
| 471 | return VersionTuple(Major); |
| 472 | } |
| 473 | |
| 474 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 475 | Diag(Tok, diag::err_expected_version); |
| 476 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 477 | return VersionTuple(); |
| 478 | } |
| 479 | |
| 480 | // Parse the minor version. |
| 481 | unsigned AfterMinor = AfterMajor + 1; |
| 482 | unsigned Minor = 0; |
| 483 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 484 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 485 | ++AfterMinor; |
| 486 | } |
| 487 | |
| 488 | if (AfterMinor == ActualLength) { |
| 489 | ConsumeToken(); |
| 490 | |
| 491 | // We had major.minor. |
| 492 | if (Major == 0 && Minor == 0) { |
| 493 | Diag(Tok, diag::err_zero_version); |
| 494 | return VersionTuple(); |
| 495 | } |
| 496 | |
| 497 | return VersionTuple(Major, Minor); |
| 498 | } |
| 499 | |
| 500 | // If what follows is not a '.', we have a problem. |
| 501 | if (ThisTokBegin[AfterMinor] != '.') { |
| 502 | Diag(Tok, diag::err_expected_version); |
| 503 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 504 | return VersionTuple(); |
| 505 | } |
| 506 | |
| 507 | // Parse the subminor version. |
| 508 | unsigned AfterSubminor = AfterMinor + 1; |
| 509 | unsigned Subminor = 0; |
| 510 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 511 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 512 | ++AfterSubminor; |
| 513 | } |
| 514 | |
| 515 | if (AfterSubminor != ActualLength) { |
| 516 | Diag(Tok, diag::err_expected_version); |
| 517 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 518 | return VersionTuple(); |
| 519 | } |
| 520 | ConsumeToken(); |
| 521 | return VersionTuple(Major, Minor, Subminor); |
| 522 | } |
| 523 | |
| 524 | /// \brief Parse the contents of the "availability" attribute. |
| 525 | /// |
| 526 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 527 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 528 | /// |
| 529 | /// platform: |
| 530 | /// identifier |
| 531 | /// |
| 532 | /// version-arg-list: |
| 533 | /// version-arg |
| 534 | /// version-arg ',' version-arg-list |
| 535 | /// |
| 536 | /// version-arg: |
| 537 | /// 'introduced' '=' version |
| 538 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 539 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 540 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 541 | /// opt-message: |
| 542 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 543 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 544 | SourceLocation AvailabilityLoc, |
| 545 | ParsedAttributes &attrs, |
| 546 | SourceLocation *endLoc) { |
| 547 | SourceLocation PlatformLoc; |
| 548 | IdentifierInfo *Platform = 0; |
| 549 | |
| 550 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 551 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 552 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 553 | |
| 554 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 555 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 556 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 557 | Diag(Tok, diag::err_expected_lparen); |
| 558 | return; |
| 559 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 560 | |
| 561 | // Parse the platform name, |
| 562 | if (Tok.isNot(tok::identifier)) { |
| 563 | Diag(Tok, diag::err_availability_expected_platform); |
| 564 | SkipUntil(tok::r_paren); |
| 565 | return; |
| 566 | } |
| 567 | Platform = Tok.getIdentifierInfo(); |
| 568 | PlatformLoc = ConsumeToken(); |
| 569 | |
| 570 | // Parse the ',' following the platform name. |
| 571 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 572 | return; |
| 573 | |
| 574 | // If we haven't grabbed the pointers for the identifiers |
| 575 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 576 | if (!Ident_introduced) { |
| 577 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 578 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 579 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 580 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 581 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 585 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 586 | do { |
| 587 | if (Tok.isNot(tok::identifier)) { |
| 588 | Diag(Tok, diag::err_availability_expected_change); |
| 589 | SkipUntil(tok::r_paren); |
| 590 | return; |
| 591 | } |
| 592 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 593 | SourceLocation KeywordLoc = ConsumeToken(); |
| 594 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 595 | if (Keyword == Ident_unavailable) { |
| 596 | if (UnavailableLoc.isValid()) { |
| 597 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 598 | << Keyword << SourceRange(UnavailableLoc); |
| 599 | } |
| 600 | UnavailableLoc = KeywordLoc; |
| 601 | |
| 602 | if (Tok.isNot(tok::comma)) |
| 603 | break; |
| 604 | |
| 605 | ConsumeToken(); |
| 606 | continue; |
| 607 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 609 | if (Tok.isNot(tok::equal)) { |
| 610 | Diag(Tok, diag::err_expected_equal_after) |
| 611 | << Keyword; |
| 612 | SkipUntil(tok::r_paren); |
| 613 | return; |
| 614 | } |
| 615 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 616 | if (Keyword == Ident_message) { |
| 617 | if (!isTokenStringLiteral()) { |
| 618 | Diag(Tok, diag::err_expected_string_literal); |
| 619 | SkipUntil(tok::r_paren); |
| 620 | return; |
| 621 | } |
| 622 | MessageExpr = ParseStringLiteralExpression(); |
| 623 | break; |
| 624 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 625 | |
| 626 | SourceRange VersionRange; |
| 627 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 628 | |
| 629 | if (Version.empty()) { |
| 630 | SkipUntil(tok::r_paren); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | unsigned Index; |
| 635 | if (Keyword == Ident_introduced) |
| 636 | Index = Introduced; |
| 637 | else if (Keyword == Ident_deprecated) |
| 638 | Index = Deprecated; |
| 639 | else if (Keyword == Ident_obsoleted) |
| 640 | Index = Obsoleted; |
| 641 | else |
| 642 | Index = Unknown; |
| 643 | |
| 644 | if (Index < Unknown) { |
| 645 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 646 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 647 | << Keyword |
| 648 | << SourceRange(Changes[Index].KeywordLoc, |
| 649 | Changes[Index].VersionRange.getEnd()); |
| 650 | } |
| 651 | |
| 652 | Changes[Index].KeywordLoc = KeywordLoc; |
| 653 | Changes[Index].Version = Version; |
| 654 | Changes[Index].VersionRange = VersionRange; |
| 655 | } else { |
| 656 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 657 | << Keyword << VersionRange; |
| 658 | } |
| 659 | |
| 660 | if (Tok.isNot(tok::comma)) |
| 661 | break; |
| 662 | |
| 663 | ConsumeToken(); |
| 664 | } while (true); |
| 665 | |
| 666 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 667 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 668 | return; |
| 669 | |
| 670 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 671 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 672 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 673 | // The 'unavailable' availability cannot be combined with any other |
| 674 | // availability changes. Make sure that hasn't happened. |
| 675 | if (UnavailableLoc.isValid()) { |
| 676 | bool Complained = false; |
| 677 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 678 | if (Changes[Index].KeywordLoc.isValid()) { |
| 679 | if (!Complained) { |
| 680 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 681 | << SourceRange(Changes[Index].KeywordLoc, |
| 682 | Changes[Index].VersionRange.getEnd()); |
| 683 | Complained = true; |
| 684 | } |
| 685 | |
| 686 | // Clear out the availability. |
| 687 | Changes[Index] = AvailabilityChange(); |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 692 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 693 | attrs.addNew(&Availability, |
| 694 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 695 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 696 | Platform, PlatformLoc, |
| 697 | Changes[Introduced], |
| 698 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 699 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 700 | UnavailableLoc, MessageExpr.take(), |
| 701 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 704 | |
| 705 | // Late Parsed Attributes: |
| 706 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 707 | |
| 708 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 709 | |
| 710 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 711 | Self->ParseLexedAttributes(*Class); |
| 712 | } |
| 713 | |
| 714 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 715 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 719 | /// scope appropriately. |
| 720 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 721 | // Deal with templates |
| 722 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 723 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 724 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 725 | HasTemplateScope); |
| 726 | if (HasTemplateScope) |
| 727 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 728 | |
| 729 | // Set or update the scope flags to include Scope::ThisScope. |
| 730 | bool AlreadyHasClassScope = Class.TopLevelClass; |
| 731 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope; |
| 732 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 733 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 734 | |
| 735 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) { |
| 736 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 737 | } |
| 738 | } |
| 739 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 740 | |
| 741 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 742 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 743 | bool EnterScope, bool OnDefinition) { |
| 744 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 745 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 746 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
| 747 | } |
| 748 | LAs.clear(); |
| 749 | } |
| 750 | |
| 751 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 752 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 753 | /// This will be called at the end of parsing a class declaration |
| 754 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 755 | /// create an attribute with the arguments filled in. We add this |
| 756 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 757 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 758 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 759 | // Save the current token position. |
| 760 | SourceLocation OrigLoc = Tok.getLocation(); |
| 761 | |
| 762 | // Append the current token at the end of the new token stream so that it |
| 763 | // doesn't get lost. |
| 764 | LA.Toks.push_back(Tok); |
| 765 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 766 | // Consume the previously pushed token. |
| 767 | ConsumeAnyToken(); |
| 768 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 769 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 770 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 771 | << LA.AttrName.getName(); |
| 772 | } |
| 773 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 774 | ParsedAttributes Attrs(AttrFactory); |
| 775 | SourceLocation endLoc; |
| 776 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 777 | if (LA.Decls.size() == 1) { |
| 778 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 779 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 780 | // If the Decl is templatized, add template parameters to scope. |
| 781 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 782 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 783 | if (HasTemplateScope) |
| 784 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 785 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 786 | // If the Decl is on a function, add function parameters to the scope. |
| 787 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 788 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 789 | if (HasFunctionScope) |
| 790 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 791 | |
| 792 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 793 | |
| 794 | if (HasFunctionScope) { |
| 795 | Actions.ActOnExitFunctionContext(); |
| 796 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 797 | } |
| 798 | if (HasTemplateScope) { |
| 799 | TempScope.Exit(); |
| 800 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 801 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 802 | // If there are multiple decls, then the decl cannot be within the |
| 803 | // function scope. |
| 804 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 805 | } else { |
| 806 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 807 | } |
| 808 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 809 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 810 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 811 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 812 | |
| 813 | if (Tok.getLocation() != OrigLoc) { |
| 814 | // Due to a parsing error, we either went over the cached tokens or |
| 815 | // there are still cached tokens left, so we skip the leftover tokens. |
| 816 | // Since this is an uncommon situation that should be avoided, use the |
| 817 | // expensive isBeforeInTranslationUnit call. |
| 818 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 819 | OrigLoc)) |
| 820 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 821 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 825 | /// \brief Wrapper around a case statement checking if AttrName is |
| 826 | /// one of the thread safety attributes |
| 827 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 828 | return llvm::StringSwitch<bool>(AttrName) |
| 829 | .Case("guarded_by", true) |
| 830 | .Case("guarded_var", true) |
| 831 | .Case("pt_guarded_by", true) |
| 832 | .Case("pt_guarded_var", true) |
| 833 | .Case("lockable", true) |
| 834 | .Case("scoped_lockable", true) |
| 835 | .Case("no_thread_safety_analysis", true) |
| 836 | .Case("acquired_after", true) |
| 837 | .Case("acquired_before", true) |
| 838 | .Case("exclusive_lock_function", true) |
| 839 | .Case("shared_lock_function", true) |
| 840 | .Case("exclusive_trylock_function", true) |
| 841 | .Case("shared_trylock_function", true) |
| 842 | .Case("unlock_function", true) |
| 843 | .Case("lock_returned", true) |
| 844 | .Case("locks_excluded", true) |
| 845 | .Case("exclusive_locks_required", true) |
| 846 | .Case("shared_locks_required", true) |
| 847 | .Default(false); |
| 848 | } |
| 849 | |
| 850 | /// \brief Parse the contents of thread safety attributes. These |
| 851 | /// should always be parsed as an expression list. |
| 852 | /// |
| 853 | /// We need to special case the parsing due to the fact that if the first token |
| 854 | /// of the first argument is an identifier, the main parse loop will store |
| 855 | /// that token as a "parameter" and the rest of |
| 856 | /// the arguments will be added to a list of "arguments". However, |
| 857 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 858 | /// argument as an expression and add all arguments to the list of "arguments". |
| 859 | /// In future, we will take advantage of this special case to also |
| 860 | /// deal with some argument scoping issues here (for example, referring to a |
| 861 | /// function parameter in the attribute on that function). |
| 862 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 863 | SourceLocation AttrNameLoc, |
| 864 | ParsedAttributes &Attrs, |
| 865 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 866 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 868 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 869 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 870 | |
| 871 | ExprVector ArgExprs(Actions); |
| 872 | bool ArgExprsOk = true; |
| 873 | |
| 874 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 875 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 876 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 877 | if (ArgExpr.isInvalid()) { |
| 878 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 879 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 880 | break; |
| 881 | } else { |
| 882 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 883 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 884 | if (Tok.isNot(tok::comma)) |
| 885 | break; |
| 886 | ConsumeToken(); // Eat the comma, move to the next argument |
| 887 | } |
| 888 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 889 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 890 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 891 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 892 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 893 | if (EndLoc) |
| 894 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 895 | } |
| 896 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 897 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 898 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 899 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 902 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 903 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 904 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 905 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 906 | /// |
| 907 | /// declaration: [C99 6.7] |
| 908 | /// block-declaration -> |
| 909 | /// simple-declaration |
| 910 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 911 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 912 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 913 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 914 | /// [C++] using-declaration |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 915 | /// [C++0x/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 916 | /// others... [FIXME] |
| 917 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 918 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 919 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 920 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 921 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 922 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 923 | // Must temporarily exit the objective-c container scope for |
| 924 | // parsing c none objective-c decls. |
| 925 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 926 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 927 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 928 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 929 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 930 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 931 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 932 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 933 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 934 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 935 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 936 | // 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^] | 937 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 938 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 939 | SourceLocation InlineLoc = ConsumeToken(); |
| 940 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 941 | break; |
| 942 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 943 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 944 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 945 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 946 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 947 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 948 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 949 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 950 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 951 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 952 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 953 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 954 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 955 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 956 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 957 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 958 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 959 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 960 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 961 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 962 | // 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] | 963 | // single decl, convert it now. Alias declarations can also declare a type; |
| 964 | // include that too if it is present. |
| 965 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 969 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 970 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 971 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 972 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 973 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 974 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 975 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 976 | /// 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] | 977 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 978 | /// |
| 979 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 980 | /// of a simple-declaration. If we find that we are, we also parse the |
| 981 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 982 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 983 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 984 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 985 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 986 | bool RequireSemi, |
| 987 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 988 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 989 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 990 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 991 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 992 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 993 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 994 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 996 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 997 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 998 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 999 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1000 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1001 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1002 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1004 | |
| 1005 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1008 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1009 | /// for a declarator. |
| 1010 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1011 | switch (Tok.getKind()) { |
| 1012 | case tok::annot_cxxscope: |
| 1013 | case tok::annot_template_id: |
| 1014 | case tok::caret: |
| 1015 | case tok::code_completion: |
| 1016 | case tok::coloncolon: |
| 1017 | case tok::ellipsis: |
| 1018 | case tok::kw___attribute: |
| 1019 | case tok::kw_operator: |
| 1020 | case tok::l_paren: |
| 1021 | case tok::star: |
| 1022 | return true; |
| 1023 | |
| 1024 | case tok::amp: |
| 1025 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1026 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1027 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1028 | 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^] | 1029 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1030 | NextToken().is(tok::l_square); |
| 1031 | |
| 1032 | 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^] | 1033 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1034 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1035 | case tok::identifier: |
| 1036 | switch (NextToken().getKind()) { |
| 1037 | case tok::code_completion: |
| 1038 | case tok::coloncolon: |
| 1039 | case tok::comma: |
| 1040 | case tok::equal: |
| 1041 | case tok::equalequal: // Might be a typo for '='. |
| 1042 | case tok::kw_alignas: |
| 1043 | case tok::kw_asm: |
| 1044 | case tok::kw___attribute: |
| 1045 | case tok::l_brace: |
| 1046 | case tok::l_paren: |
| 1047 | case tok::l_square: |
| 1048 | case tok::less: |
| 1049 | case tok::r_brace: |
| 1050 | case tok::r_paren: |
| 1051 | case tok::r_square: |
| 1052 | case tok::semi: |
| 1053 | return true; |
| 1054 | |
| 1055 | case tok::colon: |
| 1056 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1057 | // and in block scope it's probably a label. Inside a class definition, |
| 1058 | // this is a bit-field. |
| 1059 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1060 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1061 | |
| 1062 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1063 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1064 | |
| 1065 | default: |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | default: |
| 1070 | return false; |
| 1071 | } |
| 1072 | } |
| 1073 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1074 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1075 | /// definition or a group of object declarations, actually parse the |
| 1076 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1077 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1078 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1079 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1080 | SourceLocation *DeclEnd, |
| 1081 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1082 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1083 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1084 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1085 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1086 | // Bail out if the first declarator didn't seem well-formed. |
| 1087 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 1088 | // Skip until ; or }. |
| 1089 | SkipUntil(tok::r_brace, true, true); |
| 1090 | if (Tok.is(tok::semi)) |
| 1091 | ConsumeToken(); |
| 1092 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1093 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1095 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1096 | // appropriate function scope after the function Decl has been constructed. |
| 1097 | LateParsedAttrList LateParsedAttrs; |
| 1098 | if (D.isFunctionDeclarator()) |
| 1099 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1100 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1101 | // Check to see if we have a function *definition* which must have a body. |
| 1102 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1103 | // Look at the next token to make sure that this isn't a function |
| 1104 | // declaration. We have to check this because __attribute__ might be the |
| 1105 | // start of a function definition in GCC-extended K&R C. |
| 1106 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1108 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1109 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1110 | Diag(Tok, diag::err_function_declared_typedef); |
| 1111 | |
| 1112 | // Recover by treating the 'typedef' as spurious. |
| 1113 | DS.ClearStorageClassSpecs(); |
| 1114 | } |
| 1115 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1116 | Decl *TheDecl = |
| 1117 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1118 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | if (isDeclarationSpecifier()) { |
| 1122 | // If there is an invalid declaration specifier right after the function |
| 1123 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1124 | // actually a body. Just fall through into the code that handles it as a |
| 1125 | // prototype, and let the top-level code handle the erroneous declspec |
| 1126 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1127 | } else { |
| 1128 | Diag(Tok, diag::err_expected_fn_body); |
| 1129 | SkipUntil(tok::semi); |
| 1130 | return DeclGroupPtrTy(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1134 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1135 | return DeclGroupPtrTy(); |
| 1136 | |
| 1137 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1138 | // must parse and analyze the for-range-initializer before the declaration is |
| 1139 | // analyzed. |
| 1140 | if (FRI && Tok.is(tok::colon)) { |
| 1141 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1142 | if (Tok.is(tok::l_brace)) |
| 1143 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1144 | else |
| 1145 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1146 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1147 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1148 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1149 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1150 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1151 | } |
| 1152 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1153 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1154 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1155 | if (LateParsedAttrs.size() > 0) |
| 1156 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1157 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1158 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1159 | DeclsInGroup.push_back(FirstDecl); |
| 1160 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1161 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1162 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1163 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1164 | // error, bail out. |
| 1165 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1166 | SourceLocation CommaLoc = ConsumeToken(); |
| 1167 | |
| 1168 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1169 | // This comma was followed by a line-break and something which can't be |
| 1170 | // the start of a declarator. The comma was probably a typo for a |
| 1171 | // semicolon. |
| 1172 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1173 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1174 | ExpectSemi = false; |
| 1175 | break; |
| 1176 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Parse the next declarator. |
| 1179 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1180 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1183 | // declaration, these would be part of the declspec. In subsequent |
| 1184 | // declarators, they become part of the declarator itself, so that they |
| 1185 | // don't apply to declarators after *this* one. Examples: |
| 1186 | // short __attribute__((common)) var; -> declspec |
| 1187 | // short var __attribute__((common)); -> declarator |
| 1188 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1189 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1190 | |
| 1191 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1192 | if (!D.isInvalidType()) { |
| 1193 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1194 | D.complete(ThisDecl); |
| 1195 | if (ThisDecl) |
| 1196 | DeclsInGroup.push_back(ThisDecl); |
| 1197 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | if (DeclEnd) |
| 1201 | *DeclEnd = Tok.getLocation(); |
| 1202 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1203 | if (ExpectSemi && |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1204 | ExpectAndConsume(tok::semi, |
| 1205 | Context == Declarator::FileContext |
| 1206 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1207 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1208 | // Okay, there was no semicolon and one was expected. If we see a |
| 1209 | // declaration specifier, just assume it was missing and continue parsing. |
| 1210 | // Otherwise things are very confused and we skip to recover. |
| 1211 | if (!isDeclarationSpecifier()) { |
| 1212 | SkipUntil(tok::r_brace, true, true); |
| 1213 | if (Tok.is(tok::semi)) |
| 1214 | ConsumeToken(); |
| 1215 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1218 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1219 | DeclsInGroup.data(), |
| 1220 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1223 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1224 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1225 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1226 | // If a simple-asm-expr is present, parse it. |
| 1227 | if (Tok.is(tok::kw_asm)) { |
| 1228 | SourceLocation Loc; |
| 1229 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1230 | if (AsmLabel.isInvalid()) { |
| 1231 | SkipUntil(tok::semi, true, true); |
| 1232 | return true; |
| 1233 | } |
| 1234 | |
| 1235 | D.setAsmLabel(AsmLabel.release()); |
| 1236 | D.SetRangeEnd(Loc); |
| 1237 | } |
| 1238 | |
| 1239 | MaybeParseGNUAttributes(D); |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1243 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1244 | /// declarator'. This method parses the remainder of the declaration |
| 1245 | /// (including any attributes or initializer, among other things) and |
| 1246 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1247 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1248 | /// init-declarator: [C99 6.7] |
| 1249 | /// declarator |
| 1250 | /// declarator '=' initializer |
| 1251 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1252 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1253 | /// [C++] declarator initializer[opt] |
| 1254 | /// |
| 1255 | /// [C++] initializer: |
| 1256 | /// [C++] '=' initializer-clause |
| 1257 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1258 | /// [C++0x] '=' 'default' [TODO] |
| 1259 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1260 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1261 | /// |
| 1262 | /// According to the standard grammar, =default and =delete are function |
| 1263 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1264 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1265 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1266 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1267 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1268 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1270 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1273 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1274 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1275 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1276 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1277 | switch (TemplateInfo.Kind) { |
| 1278 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1279 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | |
| 1282 | case ParsedTemplateInfo::Template: |
| 1283 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1284 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1285 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1286 | TemplateInfo.TemplateParams->data(), |
| 1287 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1288 | D); |
| 1289 | break; |
| 1290 | |
| 1291 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1292 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1293 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1294 | TemplateInfo.ExternLoc, |
| 1295 | TemplateInfo.TemplateLoc, |
| 1296 | D); |
| 1297 | if (ThisRes.isInvalid()) { |
| 1298 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1299 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | ThisDecl = ThisRes.get(); |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1307 | bool TypeContainsAuto = |
| 1308 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1309 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1310 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1311 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1312 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1313 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1314 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1315 | if (D.isFunctionDeclarator()) |
| 1316 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1317 | << 1 /* delete */; |
| 1318 | else |
| 1319 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1320 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1321 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1322 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1323 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1324 | else |
| 1325 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1326 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1327 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1328 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1329 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1330 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1331 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1332 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1333 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1334 | cutOffParsing(); |
| 1335 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1338 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1339 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1340 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1341 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1342 | ExitScope(); |
| 1343 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1344 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1345 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1346 | SkipUntil(tok::comma, true, true); |
| 1347 | Actions.ActOnInitializerError(ThisDecl); |
| 1348 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1349 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1350 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } else if (Tok.is(tok::l_paren)) { |
| 1353 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1354 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1355 | T.consumeOpen(); |
| 1356 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1357 | ExprVector Exprs(Actions); |
| 1358 | CommaLocsTy CommaLocs; |
| 1359 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1360 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1361 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1362 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1365 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1366 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1367 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1368 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1369 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1370 | ExitScope(); |
| 1371 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1372 | } else { |
| 1373 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1374 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1375 | |
| 1376 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1377 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1378 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1379 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1380 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1381 | ExitScope(); |
| 1382 | } |
| 1383 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1384 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1385 | T.getCloseLocation(), |
| 1386 | move_arg(Exprs)); |
| 1387 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1388 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1389 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1390 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1391 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1392 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1393 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1394 | if (D.getCXXScopeSpec().isSet()) { |
| 1395 | EnterScope(0); |
| 1396 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1397 | } |
| 1398 | |
| 1399 | ExprResult Init(ParseBraceInitializer()); |
| 1400 | |
| 1401 | if (D.getCXXScopeSpec().isSet()) { |
| 1402 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1403 | ExitScope(); |
| 1404 | } |
| 1405 | |
| 1406 | if (Init.isInvalid()) { |
| 1407 | Actions.ActOnInitializerError(ThisDecl); |
| 1408 | } else |
| 1409 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1410 | /*DirectInit=*/true, TypeContainsAuto); |
| 1411 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1412 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1413 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1416 | Actions.FinalizeDeclaration(ThisDecl); |
| 1417 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1418 | return ThisDecl; |
| 1419 | } |
| 1420 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1421 | /// ParseSpecifierQualifierList |
| 1422 | /// specifier-qualifier-list: |
| 1423 | /// type-specifier specifier-qualifier-list[opt] |
| 1424 | /// type-qualifier specifier-qualifier-list[opt] |
| 1425 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1426 | /// |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1427 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1428 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1429 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1430 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1431 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1433 | // Validate declspec for type-name. |
| 1434 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1435 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1436 | !DS.hasAttributes()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1437 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1439 | // Issue diagnostic and remove storage class if present. |
| 1440 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1441 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1442 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1443 | else |
| 1444 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1445 | DS.ClearStorageClassSpecs(); |
| 1446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1448 | // Issue diagnostic and remove function specfier if present. |
| 1449 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1450 | if (DS.isInlineSpecified()) |
| 1451 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1452 | if (DS.isVirtualSpecified()) |
| 1453 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1454 | if (DS.isExplicitSpecified()) |
| 1455 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1456 | DS.ClearFunctionSpecs(); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1460 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1461 | /// specified token is valid after the identifier in a declarator which |
| 1462 | /// immediately follows the declspec. For example, these things are valid: |
| 1463 | /// |
| 1464 | /// int x [ 4]; // direct-declarator |
| 1465 | /// int x ( int y); // direct-declarator |
| 1466 | /// int(int x ) // direct-declarator |
| 1467 | /// int x ; // simple-declaration |
| 1468 | /// int x = 17; // init-declarator-list |
| 1469 | /// int x , y; // init-declarator-list |
| 1470 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1471 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1472 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1473 | /// |
| 1474 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1475 | /// ')' happens to be valid anyway). |
| 1476 | /// int (x) |
| 1477 | /// |
| 1478 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1479 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1480 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1481 | 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] | 1482 | } |
| 1483 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1484 | |
| 1485 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1486 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1487 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1488 | /// malformed or is "implicit int" (in K&R and C89). |
| 1489 | /// |
| 1490 | /// This method handles diagnosing this prettily and returns false if the |
| 1491 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1492 | /// other pieces of declspec after it, it returns true. |
| 1493 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1494 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1495 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1496 | AccessSpecifier AS) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1497 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1499 | SourceLocation Loc = Tok.getLocation(); |
| 1500 | // If we see an identifier that is not a type name, we normally would |
| 1501 | // parse it as the identifer being declared. However, when a typename |
| 1502 | // is typo'd or the definition is not included, this will incorrectly |
| 1503 | // parse the typename as the identifier name and fall over misparsing |
| 1504 | // later parts of the diagnostic. |
| 1505 | // |
| 1506 | // As such, we try to do some look-ahead in cases where this would |
| 1507 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1508 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1509 | // an identifier with implicit int, we'd get a parse error because the |
| 1510 | // next token is obviously invalid for a type. Parse these as a case |
| 1511 | // with an invalid type specifier. |
| 1512 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1514 | // Since we know that this either implicit int (which is rare) or an |
| 1515 | // error, we'd do lookahead to try to do better recovery. |
| 1516 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 1517 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1518 | // we just avoid eating the identifier, so it will be parsed as the |
| 1519 | // identifier in the declarator. |
| 1520 | return false; |
| 1521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1523 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1524 | // error anyway. Try to recover from various common problems. Check |
| 1525 | // to see if this was a reference to a tag name without a tag specified. |
| 1526 | // 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] | 1527 | // |
| 1528 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1529 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1530 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1531 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1533 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1534 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1535 | case DeclSpec::TST_enum: |
| 1536 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1537 | case DeclSpec::TST_union: |
| 1538 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1539 | case DeclSpec::TST_struct: |
| 1540 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1541 | case DeclSpec::TST_class: |
| 1542 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1545 | if (TagName) { |
| 1546 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1547 | << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1548 | << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1550 | // Parse this as a tag as if the missing tag were present. |
| 1551 | if (TagKind == tok::kw_enum) |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1552 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1553 | else |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1554 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1555 | return true; |
| 1556 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1559 | // This is almost certainly an invalid type name. Let the action emit a |
| 1560 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1561 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1562 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1563 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1564 | // The action emitted a diagnostic, so we don't have to. |
| 1565 | if (T) { |
| 1566 | // The action has suggested that the type T could be used. Set that as |
| 1567 | // the type in the declaration specifiers, consume the would-be type |
| 1568 | // name token, and we're done. |
| 1569 | const char *PrevSpec; |
| 1570 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1571 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1572 | DS.SetRangeEnd(Tok.getLocation()); |
| 1573 | ConsumeToken(); |
| 1574 | |
| 1575 | // There may be other declaration specifiers after this. |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
| 1579 | // Fall through; the action had no suggestion for us. |
| 1580 | } else { |
| 1581 | // The action did not emit a diagnostic, so emit one now. |
| 1582 | SourceRange R; |
| 1583 | if (SS) R = SS->getRange(); |
| 1584 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1587 | // Mark this as an error. |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1588 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1589 | unsigned DiagID; |
| 1590 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1591 | DS.SetRangeEnd(Tok.getLocation()); |
| 1592 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1594 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1595 | // avoid rippling error messages on subsequent uses of the same type, |
| 1596 | // could be useful if #include was forgotten. |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1600 | /// \brief Determine the declaration specifier context from the declarator |
| 1601 | /// context. |
| 1602 | /// |
| 1603 | /// \param Context the declarator context, which is one of the |
| 1604 | /// Declarator::TheContext enumerator values. |
| 1605 | Parser::DeclSpecContext |
| 1606 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1607 | if (Context == Declarator::MemberContext) |
| 1608 | return DSC_class; |
| 1609 | if (Context == Declarator::FileContext) |
| 1610 | return DSC_top_level; |
| 1611 | return DSC_normal; |
| 1612 | } |
| 1613 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1614 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1615 | /// |
| 1616 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1617 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1618 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1619 | /// [C11] type-id |
| 1620 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1621 | /// [C++0x] type-id ...[opt] |
| 1622 | /// [C++0x] assignment-expression ...[opt] |
| 1623 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1624 | SourceLocation &EllipsisLoc) { |
| 1625 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1626 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1627 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1628 | ParsedType Ty = ParseTypeName().get(); |
| 1629 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1630 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1631 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1632 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1633 | ER = ParseConstantExpression(); |
| 1634 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1635 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1636 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1637 | |
| 1638 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1642 | /// attribute to Attrs. |
| 1643 | /// |
| 1644 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1645 | /// [C11] '_Alignas' '(' type-id ')' |
| 1646 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1647 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1648 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1649 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1650 | SourceLocation *endLoc) { |
| 1651 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1652 | "Not an alignment-specifier!"); |
| 1653 | |
| 1654 | SourceLocation KWLoc = Tok.getLocation(); |
| 1655 | ConsumeToken(); |
| 1656 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1657 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1658 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1659 | return; |
| 1660 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1661 | SourceLocation EllipsisLoc; |
| 1662 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1663 | if (ArgExpr.isInvalid()) { |
| 1664 | SkipUntil(tok::r_paren); |
| 1665 | return; |
| 1666 | } |
| 1667 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1668 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1669 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1670 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1671 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1672 | // FIXME: Handle pack-expansions here. |
| 1673 | if (EllipsisLoc.isValid()) { |
| 1674 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1675 | return; |
| 1676 | } |
| 1677 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1678 | ExprVector ArgExprs(Actions); |
| 1679 | ArgExprs.push_back(ArgExpr.release()); |
| 1680 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1681 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | /// ParseDeclarationSpecifiers |
| 1685 | /// declaration-specifiers: [C99 6.7] |
| 1686 | /// storage-class-specifier declaration-specifiers[opt] |
| 1687 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1688 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1689 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1690 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1691 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1692 | /// |
| 1693 | /// storage-class-specifier: [C99 6.7.1] |
| 1694 | /// 'typedef' |
| 1695 | /// 'extern' |
| 1696 | /// 'static' |
| 1697 | /// 'auto' |
| 1698 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1699 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1701 | /// function-specifier: [C99 6.7.4] |
| 1702 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1703 | /// [C++] 'virtual' |
| 1704 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1705 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1706 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1707 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1709 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1710 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1711 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1712 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1713 | DeclSpecContext DSContext, |
| 1714 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1715 | if (DS.getSourceRange().isInvalid()) { |
| 1716 | DS.SetRangeStart(Tok.getLocation()); |
| 1717 | DS.SetRangeEnd(Tok.getLocation()); |
| 1718 | } |
| 1719 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1720 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1721 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1722 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1723 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1724 | unsigned DiagID = 0; |
| 1725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1726 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1727 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1728 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1730 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1731 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1732 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | // If this is not a declaration specifier token, we're done reading decl |
| 1735 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1736 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1737 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1739 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1740 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1741 | if (DS.hasTypeSpecifier()) { |
| 1742 | bool AllowNonIdentifiers |
| 1743 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1744 | Scope::BlockScope | |
| 1745 | Scope::TemplateParamScope | |
| 1746 | Scope::FunctionPrototypeScope | |
| 1747 | Scope::AtCatchScope)) == 0; |
| 1748 | bool AllowNestedNameSpecifiers |
| 1749 | = DSContext == DSC_top_level || |
| 1750 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1751 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1752 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1753 | AllowNonIdentifiers, |
| 1754 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1755 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1758 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1759 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1760 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1761 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1762 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1763 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1764 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1765 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1766 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1767 | |
| 1768 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1769 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1772 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1773 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1774 | if (TryAnnotateCXXScopeToken(true)) { |
| 1775 | if (!DS.hasTypeSpecifier()) |
| 1776 | DS.SetTypeSpecError(); |
| 1777 | goto DoneWithDeclSpec; |
| 1778 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1779 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1780 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1781 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1782 | |
| 1783 | case tok::annot_cxxscope: { |
| 1784 | if (DS.hasTypeSpecifier()) |
| 1785 | goto DoneWithDeclSpec; |
| 1786 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1787 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1788 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1789 | Tok.getAnnotationRange(), |
| 1790 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1791 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1792 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1793 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1795 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1796 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1797 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1798 | |
| 1799 | // C++ [class.qual]p2: |
| 1800 | // In a lookup in which the constructor is an acceptable lookup |
| 1801 | // result and the nested-name-specifier nominates a class C: |
| 1802 | // |
| 1803 | // - if the name specified after the |
| 1804 | // nested-name-specifier, when looked up in C, is the |
| 1805 | // injected-class-name of C (Clause 9), or |
| 1806 | // |
| 1807 | // - if the name specified after the nested-name-specifier |
| 1808 | // is the same as the identifier or the |
| 1809 | // simple-template-id's template-name in the last |
| 1810 | // component of the nested-name-specifier, |
| 1811 | // |
| 1812 | // the name is instead considered to name the constructor of |
| 1813 | // class C. |
| 1814 | // |
| 1815 | // Thus, if the template-name is actually the constructor |
| 1816 | // name, then the code is ill-formed; this interpretation is |
| 1817 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1818 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1819 | if ((DSContext == DSC_top_level || |
| 1820 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1821 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1822 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1823 | if (isConstructorDeclarator()) { |
| 1824 | // The user meant this to be an out-of-line constructor |
| 1825 | // definition, but template arguments are not allowed |
| 1826 | // there. Just allow this as a constructor; we'll |
| 1827 | // complain about it later. |
| 1828 | goto DoneWithDeclSpec; |
| 1829 | } |
| 1830 | |
| 1831 | // The user meant this to name a type, but it actually names |
| 1832 | // a constructor with some extraneous template |
| 1833 | // arguments. Complain, then parse it as a type as the user |
| 1834 | // intended. |
| 1835 | Diag(TemplateId->TemplateNameLoc, |
| 1836 | diag::err_out_of_line_template_id_names_constructor) |
| 1837 | << TemplateId->Name; |
| 1838 | } |
| 1839 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1840 | DS.getTypeSpecScope() = SS; |
| 1841 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1843 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1844 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1845 | continue; |
| 1846 | } |
| 1847 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1848 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1849 | DS.getTypeSpecScope() = SS; |
| 1850 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1851 | if (Tok.getAnnotationValue()) { |
| 1852 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1853 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1854 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1855 | PrevSpec, DiagID, T); |
| 1856 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1857 | else |
| 1858 | DS.SetTypeSpecError(); |
| 1859 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1860 | ConsumeToken(); // The typename |
| 1861 | } |
| 1862 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1863 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1864 | goto DoneWithDeclSpec; |
| 1865 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1866 | // If we're in a context where the identifier could be a class name, |
| 1867 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1868 | if ((DSContext == DSC_top_level || |
| 1869 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1870 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1871 | &SS)) { |
| 1872 | if (isConstructorDeclarator()) |
| 1873 | goto DoneWithDeclSpec; |
| 1874 | |
| 1875 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1876 | // of the class is qualified in a context where it could name |
| 1877 | // a constructor, its a constructor name. However, we've |
| 1878 | // looked at the declarator, and the user probably meant this |
| 1879 | // to be a type. Complain that it isn't supposed to be treated |
| 1880 | // as a type, then proceed to parse it as a type. |
| 1881 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1882 | << Next.getIdentifierInfo(); |
| 1883 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1884 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1885 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1886 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1887 | getCurScope(), &SS, |
| 1888 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 1889 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1890 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1891 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1892 | // If the referenced identifier is not a type, then this declspec is |
| 1893 | // erroneous: We already checked about that it has no type specifier, and |
| 1894 | // 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] | 1895 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1896 | if (TypeRep == 0) { |
| 1897 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1898 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1899 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1902 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1903 | ConsumeToken(); // The C++ scope. |
| 1904 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1905 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1906 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1907 | if (isInvalid) |
| 1908 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1910 | DS.SetRangeEnd(Tok.getLocation()); |
| 1911 | ConsumeToken(); // The typename. |
| 1912 | |
| 1913 | continue; |
| 1914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1916 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1917 | if (Tok.getAnnotationValue()) { |
| 1918 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 1919 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1920 | DiagID, T); |
| 1921 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1922 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1923 | |
| 1924 | if (isInvalid) |
| 1925 | break; |
| 1926 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1927 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1928 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1930 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1931 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1932 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1933 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1934 | ParseObjCProtocolQualifiers(DS); |
| 1935 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1936 | continue; |
| 1937 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 1939 | case tok::kw___is_signed: |
| 1940 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 1941 | // typically treats it as a trait. If we see __is_signed as it appears |
| 1942 | // in libstdc++, e.g., |
| 1943 | // |
| 1944 | // static const bool __is_signed; |
| 1945 | // |
| 1946 | // then treat __is_signed as an identifier rather than as a keyword. |
| 1947 | if (DS.getTypeSpecType() == TST_bool && |
| 1948 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 1949 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1950 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 1951 | Tok.setKind(tok::identifier); |
| 1952 | } |
| 1953 | |
| 1954 | // We're done with the declaration-specifiers. |
| 1955 | goto DoneWithDeclSpec; |
| 1956 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1957 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1958 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1959 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1960 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1961 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1962 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1963 | if (TryAnnotateCXXScopeToken(true)) { |
| 1964 | if (!DS.hasTypeSpecifier()) |
| 1965 | DS.SetTypeSpecError(); |
| 1966 | goto DoneWithDeclSpec; |
| 1967 | } |
| 1968 | if (!Tok.is(tok::identifier)) |
| 1969 | continue; |
| 1970 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1972 | // This identifier can only be a typedef name if we haven't already seen |
| 1973 | // a type-specifier. Without this check we misparse: |
| 1974 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1975 | if (DS.hasTypeSpecifier()) |
| 1976 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1978 | // Check for need to substitute AltiVec keyword tokens. |
| 1979 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1980 | break; |
| 1981 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1982 | // It has to be available as a typedef too! |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1983 | ParsedType TypeRep = |
| 1984 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1985 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1987 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1988 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1989 | if (!TypeRep) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1990 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1991 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1992 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1994 | // If we're in a context where the identifier could be a class name, |
| 1995 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 1996 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1997 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1998 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1999 | goto DoneWithDeclSpec; |
| 2000 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2001 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2002 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2003 | if (isInvalid) |
| 2004 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2006 | DS.SetRangeEnd(Tok.getLocation()); |
| 2007 | ConsumeToken(); // The identifier |
| 2008 | |
| 2009 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2010 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2011 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2012 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2013 | ParseObjCProtocolQualifiers(DS); |
| 2014 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2015 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2016 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2017 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2018 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2019 | |
| 2020 | // type-name |
| 2021 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2022 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2023 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2024 | // This template-id does not refer to a type name, so we're |
| 2025 | // done with the type-specifiers. |
| 2026 | goto DoneWithDeclSpec; |
| 2027 | } |
| 2028 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2029 | // If we're in a context where the template-id could be a |
| 2030 | // constructor name or specialization, check whether this is a |
| 2031 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2032 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2033 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2034 | isConstructorDeclarator()) |
| 2035 | goto DoneWithDeclSpec; |
| 2036 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2037 | // Turn the template-id annotation token into a type annotation |
| 2038 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2039 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2040 | continue; |
| 2041 | } |
| 2042 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2043 | // GNU attributes support. |
| 2044 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2045 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2046 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Microsoft declspec support. |
| 2049 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2050 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2051 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2053 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2054 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2055 | // FIXME: Add handling here! |
| 2056 | break; |
| 2057 | |
| 2058 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2059 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2060 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2061 | case tok::kw___cdecl: |
| 2062 | case tok::kw___stdcall: |
| 2063 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2064 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2065 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2066 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2067 | continue; |
| 2068 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2069 | // Borland single token adornments. |
| 2070 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2071 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2072 | continue; |
| 2073 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2074 | // OpenCL single token adornments. |
| 2075 | case tok::kw___kernel: |
| 2076 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2077 | continue; |
| 2078 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2079 | // storage-class-specifier |
| 2080 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2081 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2082 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2083 | break; |
| 2084 | case tok::kw_extern: |
| 2085 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2086 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2087 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2088 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2089 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2090 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2091 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2092 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2093 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2094 | case tok::kw_static: |
| 2095 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2096 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2097 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2098 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2099 | break; |
| 2100 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2101 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2102 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2103 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2104 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2105 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2106 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2107 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2108 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2109 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2110 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2111 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2112 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2113 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | break; |
| 2115 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2116 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2117 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2118 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2119 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2120 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2121 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2122 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2123 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2124 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2125 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2127 | // function-specifier |
| 2128 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2129 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2130 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2131 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2132 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2133 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2134 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2135 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2136 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2137 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2138 | // alignment-specifier |
| 2139 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2140 | if (!getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2141 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2142 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2143 | continue; |
| 2144 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2145 | // friend |
| 2146 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2147 | if (DSContext == DSC_class) |
| 2148 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2149 | else { |
| 2150 | PrevSpec = ""; // not actually used by the diagnostic |
| 2151 | DiagID = diag::err_friend_invalid_in_context; |
| 2152 | isInvalid = true; |
| 2153 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2154 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2156 | // Modules |
| 2157 | case tok::kw___module_private__: |
| 2158 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2159 | break; |
| 2160 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2161 | // constexpr |
| 2162 | case tok::kw_constexpr: |
| 2163 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2164 | break; |
| 2165 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2166 | // type-specifier |
| 2167 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2168 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2169 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | case tok::kw_long: |
| 2172 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2173 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2174 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2175 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2176 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2177 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2178 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2179 | case tok::kw___int64: |
| 2180 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2181 | DiagID); |
| 2182 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2183 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2184 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2185 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2186 | break; |
| 2187 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2188 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2189 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2190 | break; |
| 2191 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2192 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2193 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2194 | break; |
| 2195 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2196 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2197 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2198 | break; |
| 2199 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2200 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2201 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2202 | break; |
| 2203 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2204 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2205 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2208 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2209 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2210 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2211 | case tok::kw_half: |
| 2212 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2213 | DiagID); |
| 2214 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2215 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2216 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2217 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2218 | break; |
| 2219 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2220 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2221 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2222 | break; |
| 2223 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2224 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2225 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2226 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2227 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2228 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2229 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2230 | break; |
| 2231 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2232 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2233 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2234 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2235 | case tok::kw_bool: |
| 2236 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2237 | if (Tok.is(tok::kw_bool) && |
| 2238 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2239 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2240 | PrevSpec = ""; // Not used by the diagnostic. |
| 2241 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2242 | // For better error recovery. |
| 2243 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2244 | isInvalid = true; |
| 2245 | } else { |
| 2246 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2247 | DiagID); |
| 2248 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2249 | break; |
| 2250 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2251 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2252 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2253 | break; |
| 2254 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2255 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2256 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2257 | break; |
| 2258 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2259 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2260 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2261 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2262 | case tok::kw___vector: |
| 2263 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2264 | break; |
| 2265 | case tok::kw___pixel: |
| 2266 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2267 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2268 | case tok::kw___unknown_anytype: |
| 2269 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2270 | PrevSpec, DiagID); |
| 2271 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2272 | |
| 2273 | // class-specifier: |
| 2274 | case tok::kw_class: |
| 2275 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2276 | case tok::kw_union: { |
| 2277 | tok::TokenKind Kind = Tok.getKind(); |
| 2278 | ConsumeToken(); |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2279 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2280 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2281 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2282 | |
| 2283 | // enum-specifier: |
| 2284 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2285 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2286 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2287 | continue; |
| 2288 | |
| 2289 | // cv-qualifier: |
| 2290 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2291 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2292 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2293 | break; |
| 2294 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2295 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2296 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2297 | break; |
| 2298 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2299 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2300 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2303 | // C++ typename-specifier: |
| 2304 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2305 | if (TryAnnotateTypeOrScopeToken()) { |
| 2306 | DS.SetTypeSpecError(); |
| 2307 | goto DoneWithDeclSpec; |
| 2308 | } |
| 2309 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2310 | continue; |
| 2311 | break; |
| 2312 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2313 | // GNU typeof support. |
| 2314 | case tok::kw_typeof: |
| 2315 | ParseTypeofSpecifier(DS); |
| 2316 | continue; |
| 2317 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2318 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2319 | ParseDecltypeSpecifier(DS); |
| 2320 | continue; |
| 2321 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2322 | case tok::kw___underlying_type: |
| 2323 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2324 | continue; |
| 2325 | |
| 2326 | case tok::kw__Atomic: |
| 2327 | ParseAtomicSpecifier(DS); |
| 2328 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2329 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2330 | // OpenCL qualifiers: |
| 2331 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2332 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2333 | goto DoneWithDeclSpec; |
| 2334 | case tok::kw___private: |
| 2335 | case tok::kw___global: |
| 2336 | case tok::kw___local: |
| 2337 | case tok::kw___constant: |
| 2338 | case tok::kw___read_only: |
| 2339 | case tok::kw___write_only: |
| 2340 | case tok::kw___read_write: |
| 2341 | ParseOpenCLQualifiers(DS); |
| 2342 | break; |
| 2343 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2344 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2345 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2346 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2347 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2348 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2349 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2351 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2352 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2353 | << FixItHint::CreateInsertion(Loc, "id") |
| 2354 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2355 | |
| 2356 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2357 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2358 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2359 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2360 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2361 | if (isInvalid) { |
| 2362 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2363 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2364 | |
| 2365 | if (DiagID == diag::ext_duplicate_declspec) |
| 2366 | Diag(Tok, DiagID) |
| 2367 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2368 | else |
| 2369 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2370 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2372 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2373 | if (DiagID != diag::err_bool_redeclaration) |
| 2374 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2378 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2379 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 2380 | /// which together subsume the C grammar. Note that the C++ |
| 2381 | /// type-specifier also includes the C type-qualifier (for const, |
| 2382 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 2383 | /// found (and parsed), false otherwise. |
| 2384 | /// |
| 2385 | /// type-specifier: [C++ 7.1.5] |
| 2386 | /// simple-type-specifier |
| 2387 | /// class-specifier |
| 2388 | /// enum-specifier |
| 2389 | /// elaborated-type-specifier [TODO] |
| 2390 | /// cv-qualifier |
| 2391 | /// |
| 2392 | /// cv-qualifier: [C++ 7.1.5.1] |
| 2393 | /// 'const' |
| 2394 | /// 'volatile' |
| 2395 | /// [C99] 'restrict' |
| 2396 | /// |
| 2397 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 2398 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 2399 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 2400 | /// 'char' |
| 2401 | /// 'wchar_t' |
| 2402 | /// 'bool' |
| 2403 | /// 'short' |
| 2404 | /// 'int' |
| 2405 | /// 'long' |
| 2406 | /// 'signed' |
| 2407 | /// 'unsigned' |
| 2408 | /// 'float' |
| 2409 | /// 'double' |
| 2410 | /// 'void' |
| 2411 | /// [C99] '_Bool' |
| 2412 | /// [C99] '_Complex' |
| 2413 | /// [C99] '_Imaginary' // Removed in TC2? |
| 2414 | /// [GNU] '_Decimal32' |
| 2415 | /// [GNU] '_Decimal64' |
| 2416 | /// [GNU] '_Decimal128' |
| 2417 | /// [GNU] typeof-specifier |
| 2418 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 2419 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2420 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2421 | /// [AltiVec] '__vector' |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2422 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2423 | const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2424 | unsigned &DiagID, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2425 | const ParsedTemplateInfo &TemplateInfo, |
| 2426 | bool SuppressDeclarations) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2427 | SourceLocation Loc = Tok.getLocation(); |
| 2428 | |
| 2429 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2430 | case tok::identifier: // foo::bar |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 2431 | // If we already have a type specifier, this identifier is not a type. |
| 2432 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 2433 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 2434 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 2435 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2436 | // Check for need to substitute AltiVec keyword tokens. |
| 2437 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2438 | break; |
| 2439 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2440 | case tok::kw_decltype: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2441 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2442 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2443 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2444 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2445 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2446 | return true; |
| 2447 | if (Tok.is(tok::identifier)) |
| 2448 | return false; |
| 2449 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2450 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2451 | case tok::coloncolon: // ::foo::bar |
| 2452 | if (NextToken().is(tok::kw_new) || // ::new |
| 2453 | NextToken().is(tok::kw_delete)) // ::delete |
| 2454 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2456 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2457 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2458 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2459 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2460 | return true; |
| 2461 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2462 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2464 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2465 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2466 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2467 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 2468 | Tok.getAnnotationEndLoc(), PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2469 | DiagID, T); |
| 2470 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2471 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2472 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2473 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2475 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2476 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 2477 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 2478 | // just a normal reference to a typedef name. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2479 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2480 | ParseObjCProtocolQualifiers(DS); |
| 2481 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2482 | return true; |
| 2483 | } |
| 2484 | |
| 2485 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2486 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2487 | break; |
| 2488 | case tok::kw_long: |
| 2489 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2490 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2491 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2492 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2493 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2494 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2495 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2496 | case tok::kw___int64: |
| 2497 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2498 | DiagID); |
| 2499 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2500 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2501 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2502 | break; |
| 2503 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2504 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2505 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2506 | break; |
| 2507 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2508 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2509 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2510 | break; |
| 2511 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2512 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2513 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2514 | break; |
| 2515 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2516 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2517 | break; |
| 2518 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2519 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2520 | break; |
| 2521 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2522 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2523 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2524 | case tok::kw_half: |
| 2525 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID); |
| 2526 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2527 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2528 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2531 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2532 | break; |
| 2533 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2534 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2535 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2536 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2537 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2538 | break; |
| 2539 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2540 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2541 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2542 | case tok::kw_bool: |
| 2543 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2544 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2545 | break; |
| 2546 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2547 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2548 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2551 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2552 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2553 | break; |
| 2554 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2555 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2556 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2557 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2558 | case tok::kw___vector: |
| 2559 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2560 | break; |
| 2561 | case tok::kw___pixel: |
| 2562 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2563 | break; |
| 2564 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2565 | // class-specifier: |
| 2566 | case tok::kw_class: |
| 2567 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2568 | case tok::kw_union: { |
| 2569 | tok::TokenKind Kind = Tok.getKind(); |
| 2570 | ConsumeToken(); |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2571 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2572 | /*EnteringContext=*/false, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2573 | SuppressDeclarations); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2574 | return true; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2575 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2576 | |
| 2577 | // enum-specifier: |
| 2578 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2579 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2580 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2581 | return true; |
| 2582 | |
| 2583 | // cv-qualifier: |
| 2584 | case tok::kw_const: |
| 2585 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2586 | DiagID, getLangOpts()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2587 | break; |
| 2588 | case tok::kw_volatile: |
| 2589 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2590 | DiagID, getLangOpts()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | case tok::kw_restrict: |
| 2593 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2594 | DiagID, getLangOpts()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2595 | break; |
| 2596 | |
| 2597 | // GNU typeof support. |
| 2598 | case tok::kw_typeof: |
| 2599 | ParseTypeofSpecifier(DS); |
| 2600 | return true; |
| 2601 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2602 | // C++0x decltype support. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2603 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2604 | ParseDecltypeSpecifier(DS); |
| 2605 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2607 | // C++0x type traits support. |
| 2608 | case tok::kw___underlying_type: |
| 2609 | ParseUnderlyingTypeSpecifier(DS); |
| 2610 | return true; |
| 2611 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2612 | case tok::kw__Atomic: |
| 2613 | ParseAtomicSpecifier(DS); |
| 2614 | return true; |
| 2615 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2616 | // OpenCL qualifiers: |
| 2617 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2618 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2619 | return false; |
| 2620 | case tok::kw___private: |
| 2621 | case tok::kw___global: |
| 2622 | case tok::kw___local: |
| 2623 | case tok::kw___constant: |
| 2624 | case tok::kw___read_only: |
| 2625 | case tok::kw___write_only: |
| 2626 | case tok::kw___read_write: |
| 2627 | ParseOpenCLQualifiers(DS); |
| 2628 | break; |
| 2629 | |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2630 | // C++0x auto support. |
| 2631 | case tok::kw_auto: |
Richard Smith | 87e96eb | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 2632 | // This is only called in situations where a storage-class specifier is |
| 2633 | // illegal, so we can assume an auto type specifier was intended even in |
| 2634 | // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate |
| 2635 | // extension diagnostic. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2636 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2637 | return false; |
| 2638 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2639 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2640 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2641 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2642 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2643 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2644 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2645 | case tok::kw___cdecl: |
| 2646 | case tok::kw___stdcall: |
| 2647 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2648 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2649 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2650 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 2651 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2652 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2653 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2654 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2655 | return true; |
| 2656 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2657 | default: |
| 2658 | // Not a type-specifier; do nothing. |
| 2659 | return false; |
| 2660 | } |
| 2661 | |
| 2662 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2663 | if (isInvalid) { |
| 2664 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2665 | // Pick between error or extwarn. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2666 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2667 | } |
| 2668 | DS.SetRangeEnd(Tok.getLocation()); |
| 2669 | ConsumeToken(); // whatever we parsed above. |
| 2670 | return true; |
| 2671 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2672 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2673 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2674 | /// semicolon. |
| 2675 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2676 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2677 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2678 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2679 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2680 | /// struct-declarator-list: |
| 2681 | /// struct-declarator |
| 2682 | /// struct-declarator-list ',' struct-declarator |
| 2683 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2684 | /// struct-declarator: |
| 2685 | /// declarator |
| 2686 | /// [GNU] declarator attributes[opt] |
| 2687 | /// declarator[opt] ':' constant-expression |
| 2688 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2689 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2690 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2691 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2693 | if (Tok.is(tok::kw___extension__)) { |
| 2694 | // __extension__ silences extension warnings in the subexpression. |
| 2695 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2696 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2697 | return ParseStructDeclaration(DS, Fields); |
| 2698 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2700 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2701 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2703 | // If there are no declarators, this is a free-standing declaration |
| 2704 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2705 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2706 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2707 | return; |
| 2708 | } |
| 2709 | |
| 2710 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2711 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2712 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2713 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2714 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2715 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2716 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2717 | |
| 2718 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2719 | if (!FirstDeclarator) |
| 2720 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2722 | /// struct-declarator: declarator |
| 2723 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2724 | if (Tok.isNot(tok::colon)) { |
| 2725 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2726 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2727 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2730 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2731 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2732 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2733 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2734 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2735 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2736 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2737 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2738 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2739 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2740 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2741 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2742 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2743 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2744 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2745 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2746 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2747 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2748 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2749 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2750 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2751 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2752 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2753 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2754 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2755 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /// ParseStructUnionBody |
| 2759 | /// struct-contents: |
| 2760 | /// struct-declaration-list |
| 2761 | /// [EXT] empty |
| 2762 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2763 | /// struct-declaration-list: |
| 2764 | /// struct-declaration |
| 2765 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2766 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2767 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2768 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2769 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2770 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2771 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2773 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2774 | if (T.consumeOpen()) |
| 2775 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2777 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2778 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2779 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2780 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2781 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2782 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2783 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2784 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2785 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2787 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2788 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2789 | // 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] | 2790 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2791 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2793 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2794 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2795 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2796 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2797 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2798 | ConsumeToken(); |
| 2799 | continue; |
| 2800 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2803 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2805 | if (!Tok.is(tok::at)) { |
| 2806 | struct CFieldCallback : FieldCallback { |
| 2807 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2808 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2809 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2810 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2811 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2812 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2813 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2814 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2815 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2816 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2817 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2818 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2819 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2820 | FieldDecls.push_back(Field); |
| 2821 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2822 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2823 | } Callback(*this, TagDecl, FieldDecls); |
| 2824 | |
| 2825 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2826 | } else { // Handle @defs |
| 2827 | ConsumeToken(); |
| 2828 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2829 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2830 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2831 | continue; |
| 2832 | } |
| 2833 | ConsumeToken(); |
| 2834 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2835 | if (!Tok.is(tok::identifier)) { |
| 2836 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2837 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2838 | continue; |
| 2839 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2840 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2841 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2842 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2843 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2844 | ConsumeToken(); |
| 2845 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2848 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2849 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2850 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2851 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | break; |
| 2853 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2854 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2855 | // 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] | 2856 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2857 | // If we stopped at a ';', eat it. |
| 2858 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2862 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2864 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2865 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2866 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2867 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2868 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2869 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2870 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2871 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2872 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2873 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2874 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2877 | /// ParseEnumSpecifier |
| 2878 | /// enum-specifier: [C99 6.7.2.2] |
| 2879 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2880 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2881 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2882 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2883 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2884 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2885 | /// 'enum' identifier |
| 2886 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2887 | /// |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2888 | /// [C++0x] enum-head '{' enumerator-list[opt] '}' |
| 2889 | /// [C++0x] enum-head '{' enumerator-list ',' '}' |
| 2890 | /// |
| 2891 | /// enum-head: [C++0x] |
| 2892 | /// enum-key attributes[opt] identifier[opt] enum-base[opt] |
| 2893 | /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt] |
| 2894 | /// |
| 2895 | /// enum-key: [C++0x] |
| 2896 | /// 'enum' |
| 2897 | /// 'enum' 'class' |
| 2898 | /// 'enum' 'struct' |
| 2899 | /// |
| 2900 | /// enum-base: [C++0x] |
| 2901 | /// ':' type-specifier-seq |
| 2902 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2903 | /// [C++] elaborated-type-specifier: |
| 2904 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2905 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2906 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2907 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2908 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2909 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2910 | if (Tok.is(tok::code_completion)) { |
| 2911 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2912 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2913 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2914 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2915 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2916 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2917 | bool IsScopedUsingClassTag = false; |
| 2918 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2919 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2920 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2921 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2922 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2923 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2924 | } |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2925 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2926 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2927 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2928 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2929 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2930 | // If declspecs exist after tag, parse them. |
| 2931 | while (Tok.is(tok::kw___declspec)) |
| 2932 | ParseMicrosoftDeclSpec(attrs); |
| 2933 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 2934 | bool AllowFixedUnderlyingType |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2935 | = getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || getLangOpts().ObjC2; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2936 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2937 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 2938 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2939 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2940 | // if a fixed underlying type is allowed. |
| 2941 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2942 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2943 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2944 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2945 | return; |
| 2946 | |
| 2947 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2948 | Diag(Tok, diag::err_expected_ident); |
| 2949 | if (Tok.isNot(tok::l_brace)) { |
| 2950 | // Has no name and is not a definition. |
| 2951 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2952 | SkipUntil(tok::comma, true); |
| 2953 | return; |
| 2954 | } |
| 2955 | } |
| 2956 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2957 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2958 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2959 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
| 2960 | (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2961 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2963 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2964 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2965 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2968 | // If an identifier is present, consume and remember it. |
| 2969 | IdentifierInfo *Name = 0; |
| 2970 | SourceLocation NameLoc; |
| 2971 | if (Tok.is(tok::identifier)) { |
| 2972 | Name = Tok.getIdentifierInfo(); |
| 2973 | NameLoc = ConsumeToken(); |
| 2974 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2976 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2977 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2978 | // declaration of a scoped enumeration. |
| 2979 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2980 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2981 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | TypeResult BaseType; |
| 2985 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2986 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2987 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2988 | bool PossibleBitfield = false; |
| 2989 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2990 | // If we're in class scope, this can either be an enum declaration with |
| 2991 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2992 | // use a simple disambiguation scheme first to catch the common cases |
| 2993 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2994 | // anything that's a simple-type-specifier followed by '(' as an |
| 2995 | // expression. This suffices because function types are not valid |
| 2996 | // underlying types anyway. |
| 2997 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2998 | // If the next token starts an expression, we know we're parsing a |
| 2999 | // bit-field. This is the common case. |
| 3000 | if (TPR == TPResult::True()) |
| 3001 | PossibleBitfield = true; |
| 3002 | // If the next token starts a type-specifier-seq, it may be either a |
| 3003 | // a fixed underlying type or the start of a function-style cast in C++; |
| 3004 | // lookahead one more token to see if it's obvious that we have a |
| 3005 | // fixed underlying type. |
| 3006 | else if (TPR == TPResult::False() && |
| 3007 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3008 | // Consume the ':'. |
| 3009 | ConsumeToken(); |
| 3010 | } else { |
| 3011 | // We have the start of a type-specifier-seq, so we have to perform |
| 3012 | // tentative parsing to determine whether we have an expression or a |
| 3013 | // type. |
| 3014 | TentativeParsingAction TPA(*this); |
| 3015 | |
| 3016 | // Consume the ':'. |
| 3017 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3018 | |
| 3019 | // If we see a type specifier followed by an open-brace, we have an |
| 3020 | // ambiguity between an underlying type and a C++11 braced |
| 3021 | // function-style cast. Resolve this by always treating it as an |
| 3022 | // underlying type. |
| 3023 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3024 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3025 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3026 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3027 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3028 | // We'll parse this as a bitfield later. |
| 3029 | PossibleBitfield = true; |
| 3030 | TPA.Revert(); |
| 3031 | } else { |
| 3032 | // We have a type-specifier-seq. |
| 3033 | TPA.Commit(); |
| 3034 | } |
| 3035 | } |
| 3036 | } else { |
| 3037 | // Consume the ':'. |
| 3038 | ConsumeToken(); |
| 3039 | } |
| 3040 | |
| 3041 | if (!PossibleBitfield) { |
| 3042 | SourceRange Range; |
| 3043 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3044 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3045 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3046 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 3047 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3048 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3049 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3050 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3053 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3054 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3055 | // 'enum foo;', then this is a forward declaration. If we have |
| 3056 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3057 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3058 | // |
| 3059 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3060 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3061 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3062 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3063 | Sema::TagUseKind TUK; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3064 | if (DS.isFriendSpecified()) |
| 3065 | TUK = Sema::TUK_Friend; |
| 3066 | else if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3067 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3068 | else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3069 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3070 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3071 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3072 | |
| 3073 | // enums cannot be templates, although they can be referenced from a |
| 3074 | // template. |
| 3075 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3076 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3077 | Diag(Tok, diag::err_enum_template); |
| 3078 | |
| 3079 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3080 | SkipUntil(tok::comma, true); |
| 3081 | return; |
| 3082 | } |
| 3083 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3084 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3085 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
| 3086 | |
| 3087 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3088 | SkipUntil(tok::comma, true); |
| 3089 | return; |
| 3090 | } |
| 3091 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3092 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3093 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3094 | const char *PrevSpec = 0; |
| 3095 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3096 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3097 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 3098 | AS, DS.getModulePrivateSpecLoc(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3099 | MultiTemplateParamsArg(Actions), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3100 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3101 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3103 | if (IsDependent) { |
| 3104 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 3105 | // dependent tag. |
| 3106 | if (!Name) { |
| 3107 | DS.SetTypeSpecError(); |
| 3108 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3109 | return; |
| 3110 | } |
| 3111 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3112 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3113 | TUK, SS, Name, StartLoc, |
| 3114 | NameLoc); |
| 3115 | if (Type.isInvalid()) { |
| 3116 | DS.SetTypeSpecError(); |
| 3117 | return; |
| 3118 | } |
| 3119 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3120 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3121 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3122 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3123 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3124 | |
| 3125 | return; |
| 3126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3128 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3129 | // The action failed to produce an enumeration tag. If this is a |
| 3130 | // definition, consume the entire definition. |
| 3131 | if (Tok.is(tok::l_brace)) { |
| 3132 | ConsumeBrace(); |
| 3133 | SkipUntil(tok::r_brace); |
| 3134 | } |
| 3135 | |
| 3136 | DS.SetTypeSpecError(); |
| 3137 | return; |
| 3138 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3139 | |
| 3140 | if (Tok.is(tok::l_brace)) { |
| 3141 | if (TUK == Sema::TUK_Friend) |
| 3142 | Diag(Tok, diag::err_friend_decl_defines_type) |
| 3143 | << SourceRange(DS.getFriendSpecLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3144 | ParseEnumBody(StartLoc, TagDecl); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3147 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3148 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3149 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3150 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3154 | /// enumerator-list: |
| 3155 | /// enumerator |
| 3156 | /// enumerator-list ',' enumerator |
| 3157 | /// enumerator: |
| 3158 | /// enumeration-constant |
| 3159 | /// enumeration-constant '=' constant-expression |
| 3160 | /// enumeration-constant: |
| 3161 | /// identifier |
| 3162 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3163 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3164 | // Enter the scope of the enum body and start the definition. |
| 3165 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3166 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3167 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3168 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3169 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3171 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3172 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3173 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3175 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3176 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3177 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3179 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3180 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3181 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3182 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3184 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3185 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3186 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3188 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3189 | ExprResult AssignedVal; |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3190 | ParsingDeclRAIIObject PD(*this); |
| 3191 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3192 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3193 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3194 | AssignedVal = ParseConstantExpression(); |
| 3195 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3196 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3199 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3200 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3201 | LastEnumConstDecl, |
| 3202 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3203 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3204 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3205 | PD.complete(EnumConstDecl); |
| 3206 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3207 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3208 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3210 | if (Tok.is(tok::identifier)) { |
| 3211 | // We're missing a comma between enumerators. |
| 3212 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3213 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3214 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3215 | continue; |
| 3216 | } |
| 3217 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3218 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3219 | break; |
| 3220 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3222 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3223 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3224 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3225 | << getLangOpts().CPlusPlus |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3226 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3227 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3228 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3229 | << FixItHint::CreateRemoval(CommaLoc); |
| 3230 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3233 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3234 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3236 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3237 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3238 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3240 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3241 | EnumDecl, EnumConstantDecls.data(), |
| 3242 | EnumConstantDecls.size(), getCurScope(), |
| 3243 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3245 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3246 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3247 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3251 | /// start of a type-qualifier-list. |
| 3252 | bool Parser::isTypeQualifier() const { |
| 3253 | switch (Tok.getKind()) { |
| 3254 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3255 | |
| 3256 | // type-qualifier only in OpenCL |
| 3257 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3258 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3259 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3260 | // type-qualifier |
| 3261 | case tok::kw_const: |
| 3262 | case tok::kw_volatile: |
| 3263 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3264 | case tok::kw___private: |
| 3265 | case tok::kw___local: |
| 3266 | case tok::kw___global: |
| 3267 | case tok::kw___constant: |
| 3268 | case tok::kw___read_only: |
| 3269 | case tok::kw___read_write: |
| 3270 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3271 | return true; |
| 3272 | } |
| 3273 | } |
| 3274 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3275 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3276 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3277 | /// specifier or if we're not sure. |
| 3278 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3279 | switch (Tok.getKind()) { |
| 3280 | default: return false; |
| 3281 | // type-specifiers |
| 3282 | case tok::kw_short: |
| 3283 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3284 | case tok::kw___int64: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3285 | case tok::kw_signed: |
| 3286 | case tok::kw_unsigned: |
| 3287 | case tok::kw__Complex: |
| 3288 | case tok::kw__Imaginary: |
| 3289 | case tok::kw_void: |
| 3290 | case tok::kw_char: |
| 3291 | case tok::kw_wchar_t: |
| 3292 | case tok::kw_char16_t: |
| 3293 | case tok::kw_char32_t: |
| 3294 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3295 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3296 | case tok::kw_float: |
| 3297 | case tok::kw_double: |
| 3298 | case tok::kw_bool: |
| 3299 | case tok::kw__Bool: |
| 3300 | case tok::kw__Decimal32: |
| 3301 | case tok::kw__Decimal64: |
| 3302 | case tok::kw__Decimal128: |
| 3303 | case tok::kw___vector: |
| 3304 | |
| 3305 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3306 | case tok::kw_class: |
| 3307 | case tok::kw_struct: |
| 3308 | case tok::kw_union: |
| 3309 | // enum-specifier |
| 3310 | case tok::kw_enum: |
| 3311 | |
| 3312 | // typedef-name |
| 3313 | case tok::annot_typename: |
| 3314 | return true; |
| 3315 | } |
| 3316 | } |
| 3317 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3318 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3319 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3320 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3321 | switch (Tok.getKind()) { |
| 3322 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3324 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3325 | if (TryAltiVecVectorToken()) |
| 3326 | return true; |
| 3327 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3328 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3329 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3330 | // recurse to handle whatever we get. |
| 3331 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3332 | return true; |
| 3333 | if (Tok.is(tok::identifier)) |
| 3334 | return false; |
| 3335 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3337 | case tok::coloncolon: // ::foo::bar |
| 3338 | if (NextToken().is(tok::kw_new) || // ::new |
| 3339 | NextToken().is(tok::kw_delete)) // ::delete |
| 3340 | return false; |
| 3341 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3342 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3343 | return true; |
| 3344 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3346 | // GNU attributes support. |
| 3347 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3348 | // GNU typeof support. |
| 3349 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3351 | // type-specifiers |
| 3352 | case tok::kw_short: |
| 3353 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3354 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3355 | case tok::kw_signed: |
| 3356 | case tok::kw_unsigned: |
| 3357 | case tok::kw__Complex: |
| 3358 | case tok::kw__Imaginary: |
| 3359 | case tok::kw_void: |
| 3360 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3361 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3362 | case tok::kw_char16_t: |
| 3363 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3364 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3365 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3366 | case tok::kw_float: |
| 3367 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3368 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3369 | case tok::kw__Bool: |
| 3370 | case tok::kw__Decimal32: |
| 3371 | case tok::kw__Decimal64: |
| 3372 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3373 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3375 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3376 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3377 | case tok::kw_struct: |
| 3378 | case tok::kw_union: |
| 3379 | // enum-specifier |
| 3380 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3382 | // type-qualifier |
| 3383 | case tok::kw_const: |
| 3384 | case tok::kw_volatile: |
| 3385 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3386 | |
| 3387 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3388 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3389 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3391 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3392 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3393 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3395 | case tok::kw___cdecl: |
| 3396 | case tok::kw___stdcall: |
| 3397 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3398 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3399 | case tok::kw___w64: |
| 3400 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3401 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3402 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3403 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3404 | |
| 3405 | case tok::kw___private: |
| 3406 | case tok::kw___local: |
| 3407 | case tok::kw___global: |
| 3408 | case tok::kw___constant: |
| 3409 | case tok::kw___read_only: |
| 3410 | case tok::kw___read_write: |
| 3411 | case tok::kw___write_only: |
| 3412 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3413 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3414 | |
| 3415 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3416 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3417 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3418 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3419 | case tok::kw__Atomic: |
| 3420 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3425 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3426 | /// |
| 3427 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3428 | /// this check is to disambiguate between an expression and a declaration. |
| 3429 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3430 | switch (Tok.getKind()) { |
| 3431 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3433 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3434 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3435 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3436 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3437 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3438 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3439 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3440 | if (TryAltiVecVectorToken()) |
| 3441 | return true; |
| 3442 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3443 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3444 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3445 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3446 | // recurse to handle whatever we get. |
| 3447 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3448 | return true; |
| 3449 | if (Tok.is(tok::identifier)) |
| 3450 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3451 | |
| 3452 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3453 | // by an identifier and then either ':' or ']', in a place where an |
| 3454 | // expression is permitted, then this is probably a class message send |
| 3455 | // missing the initial '['. In this case, we won't consider this to be |
| 3456 | // the start of a declaration. |
| 3457 | if (DisambiguatingWithExpression && |
| 3458 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3459 | return false; |
| 3460 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3461 | return isDeclarationSpecifier(); |
| 3462 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3463 | case tok::coloncolon: // ::foo::bar |
| 3464 | if (NextToken().is(tok::kw_new) || // ::new |
| 3465 | NextToken().is(tok::kw_delete)) // ::delete |
| 3466 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3468 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3469 | // recurse to handle whatever we get. |
| 3470 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3471 | return true; |
| 3472 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3474 | // storage-class-specifier |
| 3475 | case tok::kw_typedef: |
| 3476 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3477 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3478 | case tok::kw_static: |
| 3479 | case tok::kw_auto: |
| 3480 | case tok::kw_register: |
| 3481 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3483 | // Modules |
| 3484 | case tok::kw___module_private__: |
| 3485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3486 | // type-specifiers |
| 3487 | case tok::kw_short: |
| 3488 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3489 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3490 | case tok::kw_signed: |
| 3491 | case tok::kw_unsigned: |
| 3492 | case tok::kw__Complex: |
| 3493 | case tok::kw__Imaginary: |
| 3494 | case tok::kw_void: |
| 3495 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3496 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3497 | case tok::kw_char16_t: |
| 3498 | case tok::kw_char32_t: |
| 3499 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3500 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3501 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3502 | case tok::kw_float: |
| 3503 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3504 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3505 | case tok::kw__Bool: |
| 3506 | case tok::kw__Decimal32: |
| 3507 | case tok::kw__Decimal64: |
| 3508 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3509 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3511 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3512 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3513 | case tok::kw_struct: |
| 3514 | case tok::kw_union: |
| 3515 | // enum-specifier |
| 3516 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3517 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3518 | // type-qualifier |
| 3519 | case tok::kw_const: |
| 3520 | case tok::kw_volatile: |
| 3521 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3522 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3523 | // function-specifier |
| 3524 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3525 | case tok::kw_virtual: |
| 3526 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3527 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3528 | // static_assert-declaration |
| 3529 | case tok::kw__Static_assert: |
| 3530 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3531 | // GNU typeof support. |
| 3532 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3534 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3535 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3536 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3538 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3539 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3540 | return true; |
| 3541 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3542 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3543 | case tok::kw__Atomic: |
| 3544 | return true; |
| 3545 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3546 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3547 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3548 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3550 | // typedef-name |
| 3551 | case tok::annot_typename: |
| 3552 | return !DisambiguatingWithExpression || |
| 3553 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3554 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3555 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3556 | case tok::kw___cdecl: |
| 3557 | case tok::kw___stdcall: |
| 3558 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3559 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3560 | case tok::kw___w64: |
| 3561 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3562 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3563 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3564 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3565 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3566 | |
| 3567 | case tok::kw___private: |
| 3568 | case tok::kw___local: |
| 3569 | case tok::kw___global: |
| 3570 | case tok::kw___constant: |
| 3571 | case tok::kw___read_only: |
| 3572 | case tok::kw___read_write: |
| 3573 | case tok::kw___write_only: |
| 3574 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3575 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3579 | bool Parser::isConstructorDeclarator() { |
| 3580 | TentativeParsingAction TPA(*this); |
| 3581 | |
| 3582 | // Parse the C++ scope specifier. |
| 3583 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3584 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3585 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3586 | TPA.Revert(); |
| 3587 | return false; |
| 3588 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3589 | |
| 3590 | // Parse the constructor name. |
| 3591 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3592 | // We already know that we have a constructor name; just consume |
| 3593 | // the token. |
| 3594 | ConsumeToken(); |
| 3595 | } else { |
| 3596 | TPA.Revert(); |
| 3597 | return false; |
| 3598 | } |
| 3599 | |
| 3600 | // Current class name must be followed by a left parentheses. |
| 3601 | if (Tok.isNot(tok::l_paren)) { |
| 3602 | TPA.Revert(); |
| 3603 | return false; |
| 3604 | } |
| 3605 | ConsumeParen(); |
| 3606 | |
| 3607 | // A right parentheses or ellipsis signals that we have a constructor. |
| 3608 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 3609 | TPA.Revert(); |
| 3610 | return true; |
| 3611 | } |
| 3612 | |
| 3613 | // If we need to, enter the specified scope. |
| 3614 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3615 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3616 | DeclScopeObj.EnterDeclaratorScope(); |
| 3617 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3618 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3619 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3620 | MaybeParseMicrosoftAttributes(Attrs); |
| 3621 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3622 | // Check whether the next token(s) are part of a declaration |
| 3623 | // specifier, in which case we have the start of a parameter and, |
| 3624 | // therefore, we know that this is a constructor. |
| 3625 | bool IsConstructor = isDeclarationSpecifier(); |
| 3626 | TPA.Revert(); |
| 3627 | return IsConstructor; |
| 3628 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3629 | |
| 3630 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3631 | /// type-qualifier-list: [C99 6.7.5] |
| 3632 | /// type-qualifier |
| 3633 | /// [vendor] attributes |
| 3634 | /// [ only if VendorAttributesAllowed=true ] |
| 3635 | /// type-qualifier-list type-qualifier |
| 3636 | /// [vendor] type-qualifier-list attributes |
| 3637 | /// [ only if VendorAttributesAllowed=true ] |
| 3638 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3639 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3640 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3641 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3642 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3643 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3644 | bool CXX0XAttributesAllowed) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3645 | if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3646 | SourceLocation Loc = Tok.getLocation(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3647 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3648 | ParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3649 | if (CXX0XAttributesAllowed) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3650 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3651 | else |
| 3652 | Diag(Loc, diag::err_attributes_not_allowed); |
| 3653 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3654 | |
| 3655 | SourceLocation EndLoc; |
| 3656 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3657 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3658 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3659 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3660 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3661 | SourceLocation Loc = Tok.getLocation(); |
| 3662 | |
| 3663 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3664 | case tok::code_completion: |
| 3665 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3666 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3668 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3669 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3670 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3671 | break; |
| 3672 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3673 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3674 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3675 | break; |
| 3676 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3677 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3678 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3679 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3680 | |
| 3681 | // OpenCL qualifiers: |
| 3682 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3683 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3684 | goto DoneWithTypeQuals; |
| 3685 | case tok::kw___private: |
| 3686 | case tok::kw___global: |
| 3687 | case tok::kw___local: |
| 3688 | case tok::kw___constant: |
| 3689 | case tok::kw___read_only: |
| 3690 | case tok::kw___write_only: |
| 3691 | case tok::kw___read_write: |
| 3692 | ParseOpenCLQualifiers(DS); |
| 3693 | break; |
| 3694 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3695 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3696 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3697 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3698 | case tok::kw___cdecl: |
| 3699 | case tok::kw___stdcall: |
| 3700 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3701 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3702 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3703 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3704 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3705 | continue; |
| 3706 | } |
| 3707 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3708 | case tok::kw___pascal: |
| 3709 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3710 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3711 | continue; |
| 3712 | } |
| 3713 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3714 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3715 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3716 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3717 | continue; // do *not* consume the next token! |
| 3718 | } |
| 3719 | // otherwise, FALL THROUGH! |
| 3720 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3721 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3722 | // If this is not a type-qualifier token, we're done reading type |
| 3723 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3724 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3725 | if (EndLoc.isValid()) |
| 3726 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3727 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3728 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3730 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3731 | if (isInvalid) { |
| 3732 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3733 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3734 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3735 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | |
| 3739 | |
| 3740 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3741 | /// |
| 3742 | void Parser::ParseDeclarator(Declarator &D) { |
| 3743 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3744 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3745 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3748 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3749 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3750 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3751 | /// ptr-operator production. |
| 3752 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3753 | /// If the grammar of this construct is extended, matching changes must also be |
| 3754 | /// made to TryParseDeclarator and MightBeDeclarator. |
| 3755 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3756 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3757 | /// [C] pointer[opt] direct-declarator |
| 3758 | /// [C++] direct-declarator |
| 3759 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3760 | /// |
| 3761 | /// pointer: [C99 6.7.5] |
| 3762 | /// '*' type-qualifier-list[opt] |
| 3763 | /// '*' type-qualifier-list[opt] pointer |
| 3764 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3765 | /// ptr-operator: |
| 3766 | /// '*' cv-qualifier-seq[opt] |
| 3767 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3768 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3769 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3770 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3771 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3772 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3773 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3774 | if (Diags.hasAllExtensionsSilenced()) |
| 3775 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3776 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3777 | // C++ member pointers start with a '::' or a nested-name. |
| 3778 | // Member pointers get special handling, since there's no place for the |
| 3779 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3780 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3781 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3782 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3783 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3784 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3785 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3786 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3787 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3788 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3790 | // The scope spec really belongs to the direct-declarator. |
| 3791 | D.getCXXScopeSpec() = SS; |
| 3792 | if (DirectDeclParser) |
| 3793 | (this->*DirectDeclParser)(D); |
| 3794 | return; |
| 3795 | } |
| 3796 | |
| 3797 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3798 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3799 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3800 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3801 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3802 | |
| 3803 | // Recurse to parse whatever is left. |
| 3804 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3805 | |
| 3806 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3807 | // scope. It has to catch pointers into namespace scope anyway. |
| 3808 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3809 | Loc), |
| 3810 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3811 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3812 | return; |
| 3813 | } |
| 3814 | } |
| 3815 | |
| 3816 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3817 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3818 | if (Kind != tok::star && Kind != tok::caret && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3819 | (Kind != tok::amp || !getLangOpts().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3820 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3821 | (Kind != tok::ampamp || !getLangOpts().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3822 | if (DirectDeclParser) |
| 3823 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3824 | return; |
| 3825 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3826 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3827 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3828 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3829 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3830 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3831 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3832 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3833 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3834 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3836 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3837 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3838 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3839 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3840 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3841 | if (Kind == tok::star) |
| 3842 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3843 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3844 | DS.getConstSpecLoc(), |
| 3845 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3846 | DS.getRestrictSpecLoc()), |
| 3847 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3848 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3849 | else |
| 3850 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3852 | Loc), |
| 3853 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3854 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3855 | } else { |
| 3856 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3857 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3858 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3859 | // Complain about rvalue references in C++03, but then go on and build |
| 3860 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3861 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3862 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3863 | diag::warn_cxx98_compat_rvalue_reference : |
| 3864 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3865 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3866 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3867 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3868 | // template type argument, in which case the cv-qualifiers are ignored. |
| 3869 | // |
| 3870 | // [GNU] Retricted references are allowed. |
| 3871 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3872 | // [C++0x] Attributes on references are not allowed. |
| 3873 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3874 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3875 | |
| 3876 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3877 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3878 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3879 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3880 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3881 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3882 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3886 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3887 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3888 | if (D.getNumTypeObjects() > 0) { |
| 3889 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3890 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3891 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3892 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3893 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3894 | << II; |
| 3895 | else |
| 3896 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3897 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3898 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3899 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3900 | // can go ahead and build the (technically ill-formed) |
| 3901 | // declarator: reference collapsing will take care of it. |
| 3902 | } |
| 3903 | } |
| 3904 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3905 | // 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] | 3906 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3907 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3908 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3909 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /// ParseDirectDeclarator |
| 3914 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3915 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3916 | /// '(' declarator ')' |
| 3917 | /// [GNU] '(' attributes declarator ')' |
| 3918 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3919 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3920 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3921 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3922 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3923 | /// direct-declarator '(' parameter-type-list ')' |
| 3924 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3925 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3926 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3927 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3928 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3929 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3930 | /// |
| 3931 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3932 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3933 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3934 | /// |
| 3935 | /// id-expression: [C++ 5.1] |
| 3936 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3937 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3938 | /// |
| 3939 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3941 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3942 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3943 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3944 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3945 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3946 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3947 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3948 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 3949 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3950 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3951 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3952 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3953 | D.getContext() == Declarator::MemberContext; |
| 3954 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3955 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3958 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3959 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3960 | // Change the declaration context for name lookup, until this function |
| 3961 | // is exited (and the declarator has been parsed). |
| 3962 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3965 | // C++0x [dcl.fct]p14: |
| 3966 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3967 | // of a parameter-declaration-clause without a preceding comma. In |
| 3968 | // this case, the ellipsis is parsed as part of the |
| 3969 | // abstract-declarator if the type of the parameter names a template |
| 3970 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3971 | // as part of the parameter-declaration-clause. |
| 3972 | if (Tok.is(tok::ellipsis) && |
| 3973 | !((D.getContext() == Declarator::PrototypeContext || |
| 3974 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3975 | NextToken().is(tok::r_paren) && |
| 3976 | !Actions.containsUnexpandedParameterPacks(D))) |
| 3977 | D.setEllipsisLoc(ConsumeToken()); |
| 3978 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3979 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3980 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3981 | // We found something that indicates the start of an unqualified-id. |
| 3982 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3983 | bool AllowConstructorName; |
| 3984 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3985 | AllowConstructorName = false; |
| 3986 | else if (D.getCXXScopeSpec().isSet()) |
| 3987 | AllowConstructorName = |
| 3988 | (D.getContext() == Declarator::FileContext || |
| 3989 | (D.getContext() == Declarator::MemberContext && |
| 3990 | D.getDeclSpec().isFriendSpecified())); |
| 3991 | else |
| 3992 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3993 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3994 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3995 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3996 | /*EnteringContext=*/true, |
| 3997 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3998 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3999 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4000 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4001 | D.getName()) || |
| 4002 | // Once we're past the identifier, if the scope was bad, mark the |
| 4003 | // whole declarator bad. |
| 4004 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4005 | D.SetIdentifier(0, Tok.getLocation()); |
| 4006 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4007 | } else { |
| 4008 | // Parsed the unqualified-id; update range information and move along. |
| 4009 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4010 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4011 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4012 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4013 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4014 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4015 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4016 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4017 | "There's a C++-specific check for tok::identifier above"); |
| 4018 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4019 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4020 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4021 | goto PastIdentifier; |
| 4022 | } |
| 4023 | |
| 4024 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4025 | // direct-declarator: '(' declarator ')' |
| 4026 | // direct-declarator: '(' attributes declarator ')' |
| 4027 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4028 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4029 | |
| 4030 | // If the declarator was parenthesized, we entered the declarator |
| 4031 | // scope when parsing the parenthesized declarator, then exited |
| 4032 | // the scope already. Re-enter the scope, if we need to. |
| 4033 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4034 | // If there was an error parsing parenthesized declarator, declarator |
| 4035 | // scope may have been enterred before. Don't do it again. |
| 4036 | if (!D.isInvalidType() && |
| 4037 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4038 | // Change the declaration context for name lookup, until this function |
| 4039 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4040 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4041 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4042 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4043 | // This could be something simple like "int" (in which case the declarator |
| 4044 | // portion is empty), if an abstract-declarator is allowed. |
| 4045 | D.SetIdentifier(0, Tok.getLocation()); |
| 4046 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4047 | if (D.getContext() == Declarator::MemberContext) |
| 4048 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4049 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4050 | else if (getLangOpts().CPlusPlus) |
| 4051 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4052 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4053 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4054 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4055 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4057 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4058 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4059 | assert(D.isPastIdentifier() && |
| 4060 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4061 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4062 | // Don't parse attributes unless we have an identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4063 | if (D.getIdentifier()) |
| 4064 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4065 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4066 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4067 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4068 | // Enter function-declaration scope, limiting any declarators to the |
| 4069 | // function prototype scope, including parameter declarators. |
| 4070 | ParseScope PrototypeScope(this, |
| 4071 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4072 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4073 | // In such a case, check if we actually have a function declarator; if it |
| 4074 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4075 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4076 | // When not in file scope, warn for ambiguous function declarators, just |
| 4077 | // in case the author intended it as a variable definition. |
| 4078 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4079 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4080 | break; |
| 4081 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4082 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4083 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4084 | T.consumeOpen(); |
| 4085 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4086 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4087 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4088 | ParseBracketDeclarator(D); |
| 4089 | } else { |
| 4090 | break; |
| 4091 | } |
| 4092 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4093 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4094 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4095 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4096 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4098 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4099 | /// |
| 4100 | /// direct-declarator: |
| 4101 | /// '(' declarator ')' |
| 4102 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4103 | /// direct-declarator '(' parameter-type-list ')' |
| 4104 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4105 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4106 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4107 | /// |
| 4108 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4109 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4110 | T.consumeOpen(); |
| 4111 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4112 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4114 | // Eat any attributes before we look at whether this is a grouping or function |
| 4115 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4116 | // the type being built up, for example: |
| 4117 | // int (__attribute__(()) *x)(long y) |
| 4118 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4119 | // first argument, for example: |
| 4120 | // int (__attribute__(()) int x) |
| 4121 | // In either case, we need to eat any attributes to be able to determine what |
| 4122 | // sort of paren this is. |
| 4123 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4124 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4125 | bool RequiresArg = false; |
| 4126 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4127 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4129 | // We require that the argument list (if this is a non-grouping paren) be |
| 4130 | // present even if the attribute list was empty. |
| 4131 | RequiresArg = true; |
| 4132 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4133 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4134 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4135 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4136 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4137 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4138 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4139 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4140 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4141 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4142 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4144 | // If we haven't past the identifier yet (or where the identifier would be |
| 4145 | // stored, if this is an abstract declarator), then this is probably just |
| 4146 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4147 | // this could also be the start of function arguments (consider 'void()'). |
| 4148 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4150 | if (!D.mayOmitIdentifier()) { |
| 4151 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4152 | // paren, because we haven't seen the identifier yet. |
| 4153 | isGrouping = true; |
| 4154 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4155 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4156 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 4157 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4158 | // considered to be a type, not a K&R identifier-list. |
| 4159 | isGrouping = false; |
| 4160 | } else { |
| 4161 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4162 | isGrouping = true; |
| 4163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4165 | // If this is a grouping paren, handle: |
| 4166 | // direct-declarator: '(' declarator ')' |
| 4167 | // direct-declarator: '(' attributes declarator ')' |
| 4168 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4169 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4170 | D.setGroupingParens(true); |
| 4171 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4172 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4173 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4174 | T.consumeClose(); |
| 4175 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4176 | T.getCloseLocation()), |
| 4177 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4178 | |
| 4179 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4180 | return; |
| 4181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4182 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4183 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4184 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4185 | // identifier (and remember where it would have been), then call into |
| 4186 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4187 | D.SetIdentifier(0, Tok.getLocation()); |
| 4188 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4189 | // Enter function-declaration scope, limiting any declarators to the |
| 4190 | // function prototype scope, including parameter declarators. |
| 4191 | ParseScope PrototypeScope(this, |
| 4192 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4193 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4194 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4198 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4199 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4200 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4201 | /// If attrs is non-null, then the caller parsed those arguments immediately |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4202 | /// after the open paren - they should be considered to be the first argument of |
| 4203 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 4204 | /// function is required to be present and required to not be an identifier |
| 4205 | /// list. |
| 4206 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4207 | /// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt], |
| 4208 | /// (C++0x) ref-qualifier[opt], exception-specification[opt], and |
| 4209 | /// (C++0x) trailing-return-type[opt]. |
| 4210 | /// |
| 4211 | /// [C++0x] exception-specification: |
| 4212 | /// dynamic-exception-specification |
| 4213 | /// noexcept-specification |
| 4214 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4215 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4216 | ParsedAttributes &attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4217 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4218 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4219 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4220 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4221 | // lparen is already consumed! |
| 4222 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4223 | |
| 4224 | // This should be true when the function has typed arguments. |
| 4225 | // Otherwise, it is treated as a K&R-style function. |
| 4226 | bool HasProto = false; |
| 4227 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4228 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4229 | // Remember where we see an ellipsis, if any. |
| 4230 | SourceLocation EllipsisLoc; |
| 4231 | |
| 4232 | DeclSpec DS(AttrFactory); |
| 4233 | bool RefQualifierIsLValueRef = true; |
| 4234 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4235 | SourceLocation ConstQualifierLoc; |
| 4236 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4237 | ExceptionSpecificationType ESpecType = EST_None; |
| 4238 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4239 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4240 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4241 | ExprResult NoexceptExpr; |
| 4242 | ParsedType TrailingReturnType; |
| 4243 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4244 | Actions.ActOnStartFunctionDeclarator(); |
| 4245 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4246 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4247 | if (isFunctionDeclaratorIdentifierList()) { |
| 4248 | if (RequiresArg) |
| 4249 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4250 | |
| 4251 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4252 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4253 | Tracker.consumeClose(); |
| 4254 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4255 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4256 | if (Tok.isNot(tok::r_paren)) |
| 4257 | ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc); |
| 4258 | else if (RequiresArg) |
| 4259 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4260 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4261 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4262 | |
| 4263 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4264 | Tracker.consumeClose(); |
| 4265 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4266 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4267 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4268 | MaybeParseCXX0XAttributes(attrs); |
| 4269 | |
| 4270 | // Parse cv-qualifier-seq[opt]. |
| 4271 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4272 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4273 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4274 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4275 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4276 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4277 | |
| 4278 | // Parse ref-qualifier[opt]. |
| 4279 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4280 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4281 | diag::warn_cxx98_compat_ref_qualifier : |
| 4282 | diag::ext_ref_qualifier); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4283 | |
| 4284 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4285 | RefQualifierLoc = ConsumeToken(); |
| 4286 | EndLoc = RefQualifierLoc; |
| 4287 | } |
| 4288 | |
| 4289 | // Parse exception-specification[opt]. |
| 4290 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 4291 | DynamicExceptions, |
| 4292 | DynamicExceptionRanges, |
| 4293 | NoexceptExpr); |
| 4294 | if (ESpecType != EST_None) |
| 4295 | EndLoc = ESpecRange.getEnd(); |
| 4296 | |
| 4297 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4298 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4299 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4300 | SourceRange Range; |
| 4301 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4302 | if (Range.getEnd().isValid()) |
| 4303 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4304 | } |
| 4305 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | // Remember that we parsed a function type, and remember the attributes. |
| 4309 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4310 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4311 | EllipsisLoc, |
| 4312 | ParamInfo.data(), ParamInfo.size(), |
| 4313 | DS.getTypeQualifiers(), |
| 4314 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4315 | RefQualifierLoc, ConstQualifierLoc, |
| 4316 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4317 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4318 | ESpecType, ESpecRange.getBegin(), |
| 4319 | DynamicExceptions.data(), |
| 4320 | DynamicExceptionRanges.data(), |
| 4321 | DynamicExceptions.size(), |
| 4322 | NoexceptExpr.isUsable() ? |
| 4323 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4324 | Tracker.getOpenLocation(), |
| 4325 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4326 | TrailingReturnType), |
| 4327 | attrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4328 | |
| 4329 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4333 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4334 | /// |
| 4335 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4336 | /// abstract-declarators. |
| 4337 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4338 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4339 | && Tok.is(tok::identifier) |
| 4340 | && !TryAltiVecVectorToken() |
| 4341 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4342 | // 6.7.5.3p11. |
| 4343 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4344 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4345 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4346 | // identifier lists are really rare in the brave new modern world, and |
| 4347 | // it is very common for someone to typo a type in a non-K&R style |
| 4348 | // list. If we are presented with something like: "void foo(intptr x, |
| 4349 | // float y)", we don't want to start parsing the function declarator as |
| 4350 | // though it is a K&R style declarator just because intptr is an |
| 4351 | // invalid type. |
| 4352 | // |
| 4353 | // To handle this, we check to see if the token after the first |
| 4354 | // identifier is a "," or ")". Only then do we parse it as an |
| 4355 | // identifier list. |
| 4356 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4357 | } |
| 4358 | |
| 4359 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4360 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4361 | /// |
| 4362 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4363 | /// |
| 4364 | /// identifier-list: [C99 6.7.5] |
| 4365 | /// identifier |
| 4366 | /// identifier-list ',' identifier |
| 4367 | /// |
| 4368 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4369 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4370 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4371 | // If there was no identifier specified for the declarator, either we are in |
| 4372 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4373 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4374 | // diagnose this. |
| 4375 | if (!D.getIdentifier()) |
| 4376 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4377 | |
| 4378 | // Maintain an efficient lookup of params we have seen so far. |
| 4379 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4380 | |
| 4381 | while (1) { |
| 4382 | // If this isn't an identifier, report the error and skip until ')'. |
| 4383 | if (Tok.isNot(tok::identifier)) { |
| 4384 | Diag(Tok, diag::err_expected_ident); |
| 4385 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4386 | // Forget we parsed anything. |
| 4387 | ParamInfo.clear(); |
| 4388 | return; |
| 4389 | } |
| 4390 | |
| 4391 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4392 | |
| 4393 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4394 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4395 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4396 | |
| 4397 | // Verify that the argument identifier has not already been mentioned. |
| 4398 | if (!ParamsSoFar.insert(ParmII)) { |
| 4399 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4400 | } else { |
| 4401 | // Remember this identifier in ParamInfo. |
| 4402 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4403 | Tok.getLocation(), |
| 4404 | 0)); |
| 4405 | } |
| 4406 | |
| 4407 | // Eat the identifier. |
| 4408 | ConsumeToken(); |
| 4409 | |
| 4410 | // The list continues if we see a comma. |
| 4411 | if (Tok.isNot(tok::comma)) |
| 4412 | break; |
| 4413 | ConsumeToken(); |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4418 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4419 | /// identifier list. |
| 4420 | /// |
| 4421 | /// D is the declarator being parsed. If attrs is non-null, then the caller |
| 4422 | /// parsed those arguments immediately after the open paren - they should be |
| 4423 | /// considered to be the first argument of a parameter. |
| 4424 | /// |
| 4425 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4426 | /// be the location of the ellipsis, if any was parsed. |
| 4427 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4428 | /// parameter-type-list: [C99 6.7.5] |
| 4429 | /// parameter-list |
| 4430 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4431 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4432 | /// |
| 4433 | /// parameter-list: [C99 6.7.5] |
| 4434 | /// parameter-declaration |
| 4435 | /// parameter-list ',' parameter-declaration |
| 4436 | /// |
| 4437 | /// parameter-declaration: [C99 6.7.5] |
| 4438 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4439 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4440 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4441 | /// declaration-specifiers abstract-declarator[opt] |
| 4442 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4443 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4444 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 4445 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4446 | void Parser::ParseParameterDeclarationClause( |
| 4447 | Declarator &D, |
| 4448 | ParsedAttributes &attrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4449 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4450 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4452 | while (1) { |
| 4453 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4454 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4455 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4457 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4458 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4459 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4460 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4461 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4462 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4463 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4464 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4465 | |
| 4466 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4467 | |
| 4468 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4469 | // Take them so that we only apply the attributes to the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4470 | // FIXME: If we saw an ellipsis first, this code is not reached. Are the |
| 4471 | // attributes lost? Should they even be allowed? |
| 4472 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
| 4473 | // get rid of a parameter (attrs) and this statement. It might be too much |
| 4474 | // hassle. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4475 | DS.takeAttributesFrom(attrs); |
| 4476 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4477 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4478 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4479 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4480 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4481 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4482 | ParseDeclarator(ParmDecl); |
| 4483 | |
| 4484 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4485 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4486 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4487 | // Remember this parsed parameter in ParamInfo. |
| 4488 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4490 | // DefArgToks is used when the parsing of default arguments needs |
| 4491 | // to be delayed. |
| 4492 | CachedTokens *DefArgToks = 0; |
| 4493 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4494 | // If no parameter was specified, verify that *something* was specified, |
| 4495 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4496 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4497 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4498 | // Completely missing, emit error. |
| 4499 | Diag(DSStart, diag::err_missing_param); |
| 4500 | } else { |
| 4501 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4502 | // 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] | 4503 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4504 | // Inform the actions module about the parameter declarator, so it gets |
| 4505 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4506 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4507 | |
| 4508 | // Parse the default argument, if any. We parse the default |
| 4509 | // arguments in all dialects; the semantic analysis in |
| 4510 | // ActOnParamDefaultArgument will reject the default argument in |
| 4511 | // C. |
| 4512 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4513 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4514 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4515 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4516 | if (D.getContext() == Declarator::MemberContext) { |
| 4517 | // If we're inside a class definition, cache the tokens |
| 4518 | // corresponding to the default argument. We'll actually parse |
| 4519 | // them when we see the end of the class definition. |
| 4520 | // FIXME: Templates will require something similar. |
| 4521 | // FIXME: Can we use a smart pointer for Toks? |
| 4522 | DefArgToks = new CachedTokens; |
| 4523 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4525 | /*StopAtSemi=*/true, |
| 4526 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4527 | delete DefArgToks; |
| 4528 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4529 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4530 | } else { |
| 4531 | // Mark the end of the default argument so that we know when to |
| 4532 | // stop when we parse it later on. |
| 4533 | Token DefArgEnd; |
| 4534 | DefArgEnd.startToken(); |
| 4535 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4536 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4537 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4539 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4540 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4541 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4542 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4543 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4545 | // The argument isn't actually potentially evaluated unless it is |
| 4546 | // used. |
| 4547 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4548 | Sema::PotentiallyEvaluatedIfUsed, |
| 4549 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4550 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4551 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4552 | if (DefArgResult.isInvalid()) { |
| 4553 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4554 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4555 | } else { |
| 4556 | // Inform the actions module about the default argument |
| 4557 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4558 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4559 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4560 | } |
| 4561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4562 | |
| 4563 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4564 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4565 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | // 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] | 4569 | if (Tok.isNot(tok::comma)) { |
| 4570 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4571 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4572 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4573 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4574 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4575 | // in C. Complain and provide the fix. |
| 4576 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4577 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | break; |
| 4582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4583 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4584 | // Consume the comma. |
| 4585 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4588 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4590 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4591 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4592 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4593 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4594 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 4595 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4596 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4597 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4598 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4599 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4600 | // This code does a fast path to handle some of the most obvious cases. |
| 4601 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4602 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4603 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4604 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4605 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4606 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4607 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4608 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4609 | T.getOpenLocation(), |
| 4610 | T.getCloseLocation()), |
| 4611 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4612 | return; |
| 4613 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4614 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4615 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4616 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4617 | ConsumeToken(); |
| 4618 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4619 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4620 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4621 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4623 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4624 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4625 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4626 | T.getOpenLocation(), |
| 4627 | T.getCloseLocation()), |
| 4628 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4629 | return; |
| 4630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4632 | // If valid, this location is the position where we read the 'static' keyword. |
| 4633 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4634 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4635 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4637 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4638 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4639 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4640 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4642 | // If we haven't already read 'static', check to see if there is one after the |
| 4643 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4644 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4645 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4647 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4648 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4649 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4651 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4652 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4653 | // the the token after the star is a ']'. Since stars in arrays are |
| 4654 | // infrequent, use of lookahead is not costly here. |
| 4655 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4656 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4657 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4658 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4659 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4660 | StaticLoc = SourceLocation(); // Drop the static. |
| 4661 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4662 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4663 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4664 | // Note, in C89, this production uses the constant-expr production instead |
| 4665 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4666 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4667 | // 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] | 4668 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4669 | // Parse the constant-expression or assignment-expression now (depending |
| 4670 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 4671 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4672 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4673 | } else { |
| 4674 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4675 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4676 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4677 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4678 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4680 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4681 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4682 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4683 | // If the expression was invalid, skip it. |
| 4684 | SkipUntil(tok::r_square); |
| 4685 | return; |
| 4686 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4687 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4688 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4689 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4690 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4691 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4692 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4693 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4694 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4695 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4696 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4697 | T.getOpenLocation(), |
| 4698 | T.getCloseLocation()), |
| 4699 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4702 | /// [GNU] typeof-specifier: |
| 4703 | /// typeof ( expressions ) |
| 4704 | /// typeof ( type-name ) |
| 4705 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4706 | /// |
| 4707 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4708 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4709 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4710 | SourceLocation StartLoc = ConsumeToken(); |
| 4711 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4712 | const bool hasParens = Tok.is(tok::l_paren); |
| 4713 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4714 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4715 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4716 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4717 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4718 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4719 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4720 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4721 | if (hasParens) |
| 4722 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4723 | |
| 4724 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4725 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4726 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4727 | else |
| 4728 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4730 | if (isCastExpr) { |
| 4731 | if (!CastTy) { |
| 4732 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4733 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4734 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4735 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4736 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4737 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4738 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4739 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4740 | DiagID, CastTy)) |
| 4741 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4742 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4743 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4744 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4745 | // If we get here, the operand to the typeof was an expresion. |
| 4746 | if (Operand.isInvalid()) { |
| 4747 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4748 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4749 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4750 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4751 | // We might need to transform the operand if it is potentially evaluated. |
| 4752 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4753 | if (Operand.isInvalid()) { |
| 4754 | DS.SetTypeSpecError(); |
| 4755 | return; |
| 4756 | } |
| 4757 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4758 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4759 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4760 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4761 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4762 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4763 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4764 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4765 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4766 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4767 | /// _Atomic ( type-name ) |
| 4768 | /// |
| 4769 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4770 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4771 | |
| 4772 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4773 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4774 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4775 | SkipUntil(tok::r_paren); |
| 4776 | return; |
| 4777 | } |
| 4778 | |
| 4779 | TypeResult Result = ParseTypeName(); |
| 4780 | if (Result.isInvalid()) { |
| 4781 | SkipUntil(tok::r_paren); |
| 4782 | return; |
| 4783 | } |
| 4784 | |
| 4785 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4786 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4788 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4789 | return; |
| 4790 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4791 | DS.setTypeofParensRange(T.getRange()); |
| 4792 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4793 | |
| 4794 | const char *PrevSpec = 0; |
| 4795 | unsigned DiagID; |
| 4796 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4797 | DiagID, Result.release())) |
| 4798 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4799 | } |
| 4800 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4801 | |
| 4802 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4803 | /// from TryAltiVecVectorToken. |
| 4804 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4805 | Token Next = NextToken(); |
| 4806 | switch (Next.getKind()) { |
| 4807 | default: return false; |
| 4808 | case tok::kw_short: |
| 4809 | case tok::kw_long: |
| 4810 | case tok::kw_signed: |
| 4811 | case tok::kw_unsigned: |
| 4812 | case tok::kw_void: |
| 4813 | case tok::kw_char: |
| 4814 | case tok::kw_int: |
| 4815 | case tok::kw_float: |
| 4816 | case tok::kw_double: |
| 4817 | case tok::kw_bool: |
| 4818 | case tok::kw___pixel: |
| 4819 | Tok.setKind(tok::kw___vector); |
| 4820 | return true; |
| 4821 | case tok::identifier: |
| 4822 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4823 | Tok.setKind(tok::kw___vector); |
| 4824 | return true; |
| 4825 | } |
| 4826 | return false; |
| 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4831 | const char *&PrevSpec, unsigned &DiagID, |
| 4832 | bool &isInvalid) { |
| 4833 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4834 | Token Next = NextToken(); |
| 4835 | switch (Next.getKind()) { |
| 4836 | case tok::kw_short: |
| 4837 | case tok::kw_long: |
| 4838 | case tok::kw_signed: |
| 4839 | case tok::kw_unsigned: |
| 4840 | case tok::kw_void: |
| 4841 | case tok::kw_char: |
| 4842 | case tok::kw_int: |
| 4843 | case tok::kw_float: |
| 4844 | case tok::kw_double: |
| 4845 | case tok::kw_bool: |
| 4846 | case tok::kw___pixel: |
| 4847 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4848 | return true; |
| 4849 | case tok::identifier: |
| 4850 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4851 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4852 | return true; |
| 4853 | } |
| 4854 | break; |
| 4855 | default: |
| 4856 | break; |
| 4857 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4858 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4859 | DS.isTypeAltiVecVector()) { |
| 4860 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4861 | return true; |
| 4862 | } |
| 4863 | return false; |
| 4864 | } |