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()); |
| 273 | if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 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 |
| 539 | /// 'removed' = 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) { |
| 745 | LAs[i]->setDecl(D); |
| 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 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 777 | // If the Decl is templatized, add template parameters to scope. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 778 | bool HasTemplateScope = EnterScope && LA.D && LA.D->isTemplateDecl(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 779 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 780 | if (HasTemplateScope) |
| 781 | Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D); |
| 782 | |
| 783 | // If the Decl is on a function, add function parameters to the scope. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 784 | bool HasFunctionScope = EnterScope && LA.D && |
| 785 | LA.D->isFunctionOrFunctionTemplate(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 786 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 787 | if (HasFunctionScope) |
| 788 | Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D); |
| 789 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 790 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 791 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 792 | if (HasFunctionScope) { |
| 793 | Actions.ActOnExitFunctionContext(); |
| 794 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 795 | } |
| 796 | if (HasTemplateScope) { |
| 797 | TempScope.Exit(); |
| 798 | } |
| 799 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 800 | // Late parsed attributes must be attached to Decls by hand. If the |
| 801 | // LA.D is not set, then this was not done properly. |
| 802 | assert(LA.D && "No decl attached to late parsed attribute"); |
| 803 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs); |
| 804 | |
| 805 | if (Tok.getLocation() != OrigLoc) { |
| 806 | // Due to a parsing error, we either went over the cached tokens or |
| 807 | // there are still cached tokens left, so we skip the leftover tokens. |
| 808 | // Since this is an uncommon situation that should be avoided, use the |
| 809 | // expensive isBeforeInTranslationUnit call. |
| 810 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 811 | OrigLoc)) |
| 812 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
| 813 | ConsumeAnyToken(); |
| 814 | } |
| 815 | } |
| 816 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 817 | /// \brief Wrapper around a case statement checking if AttrName is |
| 818 | /// one of the thread safety attributes |
| 819 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 820 | return llvm::StringSwitch<bool>(AttrName) |
| 821 | .Case("guarded_by", true) |
| 822 | .Case("guarded_var", true) |
| 823 | .Case("pt_guarded_by", true) |
| 824 | .Case("pt_guarded_var", true) |
| 825 | .Case("lockable", true) |
| 826 | .Case("scoped_lockable", true) |
| 827 | .Case("no_thread_safety_analysis", true) |
| 828 | .Case("acquired_after", true) |
| 829 | .Case("acquired_before", true) |
| 830 | .Case("exclusive_lock_function", true) |
| 831 | .Case("shared_lock_function", true) |
| 832 | .Case("exclusive_trylock_function", true) |
| 833 | .Case("shared_trylock_function", true) |
| 834 | .Case("unlock_function", true) |
| 835 | .Case("lock_returned", true) |
| 836 | .Case("locks_excluded", true) |
| 837 | .Case("exclusive_locks_required", true) |
| 838 | .Case("shared_locks_required", true) |
| 839 | .Default(false); |
| 840 | } |
| 841 | |
| 842 | /// \brief Parse the contents of thread safety attributes. These |
| 843 | /// should always be parsed as an expression list. |
| 844 | /// |
| 845 | /// We need to special case the parsing due to the fact that if the first token |
| 846 | /// of the first argument is an identifier, the main parse loop will store |
| 847 | /// that token as a "parameter" and the rest of |
| 848 | /// the arguments will be added to a list of "arguments". However, |
| 849 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 850 | /// argument as an expression and add all arguments to the list of "arguments". |
| 851 | /// In future, we will take advantage of this special case to also |
| 852 | /// deal with some argument scoping issues here (for example, referring to a |
| 853 | /// function parameter in the attribute on that function). |
| 854 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 855 | SourceLocation AttrNameLoc, |
| 856 | ParsedAttributes &Attrs, |
| 857 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 858 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 860 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 861 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 862 | |
| 863 | ExprVector ArgExprs(Actions); |
| 864 | bool ArgExprsOk = true; |
| 865 | |
| 866 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 867 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 868 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 869 | if (ArgExpr.isInvalid()) { |
| 870 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 871 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 872 | break; |
| 873 | } else { |
| 874 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 875 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 876 | if (Tok.isNot(tok::comma)) |
| 877 | break; |
| 878 | ConsumeToken(); // Eat the comma, move to the next argument |
| 879 | } |
| 880 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 881 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 882 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 883 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 884 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 885 | if (EndLoc) |
| 886 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 887 | } |
| 888 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 889 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 890 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 891 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 895 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 896 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 897 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 898 | /// |
| 899 | /// declaration: [C99 6.7] |
| 900 | /// block-declaration -> |
| 901 | /// simple-declaration |
| 902 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 903 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 904 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 905 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 906 | /// [C++] using-declaration |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 907 | /// [C++0x/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 908 | /// others... [FIXME] |
| 909 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 910 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 911 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 912 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 913 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 914 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 915 | // Must temporarily exit the objective-c container scope for |
| 916 | // parsing c none objective-c decls. |
| 917 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 918 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 919 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 920 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 921 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 922 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 923 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 924 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 925 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 926 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 927 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 928 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
| 929 | if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 930 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 931 | SourceLocation InlineLoc = ConsumeToken(); |
| 932 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 933 | break; |
| 934 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 935 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 936 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 937 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 938 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 939 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 940 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 941 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 942 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 943 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 944 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 945 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 946 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 947 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 948 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 949 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 950 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 951 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 952 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 953 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 954 | // 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] | 955 | // single decl, convert it now. Alias declarations can also declare a type; |
| 956 | // include that too if it is present. |
| 957 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 961 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 962 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 963 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 964 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 965 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 966 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 967 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 968 | /// 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] | 969 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 970 | /// |
| 971 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 972 | /// of a simple-declaration. If we find that we are, we also parse the |
| 973 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 974 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 975 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 976 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 977 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 978 | bool RequireSemi, |
| 979 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 981 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 982 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 984 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 985 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 986 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 987 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 988 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 989 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 990 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 991 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 992 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 993 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 994 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 996 | |
| 997 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 998 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1000 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1001 | /// for a declarator. |
| 1002 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1003 | switch (Tok.getKind()) { |
| 1004 | case tok::annot_cxxscope: |
| 1005 | case tok::annot_template_id: |
| 1006 | case tok::caret: |
| 1007 | case tok::code_completion: |
| 1008 | case tok::coloncolon: |
| 1009 | case tok::ellipsis: |
| 1010 | case tok::kw___attribute: |
| 1011 | case tok::kw_operator: |
| 1012 | case tok::l_paren: |
| 1013 | case tok::star: |
| 1014 | return true; |
| 1015 | |
| 1016 | case tok::amp: |
| 1017 | case tok::ampamp: |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1018 | return getLang().CPlusPlus; |
| 1019 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1020 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
| 1021 | return Context == Declarator::MemberContext && getLang().CPlusPlus0x && |
| 1022 | NextToken().is(tok::l_square); |
| 1023 | |
| 1024 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
| 1025 | return Context == Declarator::MemberContext || getLang().CPlusPlus; |
| 1026 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1027 | case tok::identifier: |
| 1028 | switch (NextToken().getKind()) { |
| 1029 | case tok::code_completion: |
| 1030 | case tok::coloncolon: |
| 1031 | case tok::comma: |
| 1032 | case tok::equal: |
| 1033 | case tok::equalequal: // Might be a typo for '='. |
| 1034 | case tok::kw_alignas: |
| 1035 | case tok::kw_asm: |
| 1036 | case tok::kw___attribute: |
| 1037 | case tok::l_brace: |
| 1038 | case tok::l_paren: |
| 1039 | case tok::l_square: |
| 1040 | case tok::less: |
| 1041 | case tok::r_brace: |
| 1042 | case tok::r_paren: |
| 1043 | case tok::r_square: |
| 1044 | case tok::semi: |
| 1045 | return true; |
| 1046 | |
| 1047 | case tok::colon: |
| 1048 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1049 | // and in block scope it's probably a label. Inside a class definition, |
| 1050 | // this is a bit-field. |
| 1051 | return Context == Declarator::MemberContext || |
| 1052 | (getLang().CPlusPlus && Context == Declarator::FileContext); |
| 1053 | |
| 1054 | case tok::identifier: // Possible virt-specifier. |
| 1055 | return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1056 | |
| 1057 | default: |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | default: |
| 1062 | return false; |
| 1063 | } |
| 1064 | } |
| 1065 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1066 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1067 | /// definition or a group of object declarations, actually parse the |
| 1068 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1069 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1070 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1071 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1072 | SourceLocation *DeclEnd, |
| 1073 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1074 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1075 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1076 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1077 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1078 | // Bail out if the first declarator didn't seem well-formed. |
| 1079 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 1080 | // Skip until ; or }. |
| 1081 | SkipUntil(tok::r_brace, true, true); |
| 1082 | if (Tok.is(tok::semi)) |
| 1083 | ConsumeToken(); |
| 1084 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1085 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1087 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1088 | // appropriate function scope after the function Decl has been constructed. |
| 1089 | LateParsedAttrList LateParsedAttrs; |
| 1090 | if (D.isFunctionDeclarator()) |
| 1091 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1092 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1093 | // Check to see if we have a function *definition* which must have a body. |
| 1094 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1095 | // Look at the next token to make sure that this isn't a function |
| 1096 | // declaration. We have to check this because __attribute__ might be the |
| 1097 | // start of a function definition in GCC-extended K&R C. |
| 1098 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1100 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1101 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1102 | Diag(Tok, diag::err_function_declared_typedef); |
| 1103 | |
| 1104 | // Recover by treating the 'typedef' as spurious. |
| 1105 | DS.ClearStorageClassSpecs(); |
| 1106 | } |
| 1107 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1108 | Decl *TheDecl = |
| 1109 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1110 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | if (isDeclarationSpecifier()) { |
| 1114 | // If there is an invalid declaration specifier right after the function |
| 1115 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1116 | // actually a body. Just fall through into the code that handles it as a |
| 1117 | // prototype, and let the top-level code handle the erroneous declspec |
| 1118 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1119 | } else { |
| 1120 | Diag(Tok, diag::err_expected_fn_body); |
| 1121 | SkipUntil(tok::semi); |
| 1122 | return DeclGroupPtrTy(); |
| 1123 | } |
| 1124 | } |
| 1125 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1126 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1127 | return DeclGroupPtrTy(); |
| 1128 | |
| 1129 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1130 | // must parse and analyze the for-range-initializer before the declaration is |
| 1131 | // analyzed. |
| 1132 | if (FRI && Tok.is(tok::colon)) { |
| 1133 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1134 | if (Tok.is(tok::l_brace)) |
| 1135 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1136 | else |
| 1137 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1138 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1139 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1140 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1141 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1142 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1143 | } |
| 1144 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1145 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1146 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1147 | if (LateParsedAttrs.size() > 0) |
| 1148 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1149 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1150 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1151 | DeclsInGroup.push_back(FirstDecl); |
| 1152 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1153 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1154 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1155 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1156 | // error, bail out. |
| 1157 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1158 | SourceLocation CommaLoc = ConsumeToken(); |
| 1159 | |
| 1160 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1161 | // This comma was followed by a line-break and something which can't be |
| 1162 | // the start of a declarator. The comma was probably a typo for a |
| 1163 | // semicolon. |
| 1164 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1165 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1166 | ExpectSemi = false; |
| 1167 | break; |
| 1168 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1169 | |
| 1170 | // Parse the next declarator. |
| 1171 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1172 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1173 | |
| 1174 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1175 | // declaration, these would be part of the declspec. In subsequent |
| 1176 | // declarators, they become part of the declarator itself, so that they |
| 1177 | // don't apply to declarators after *this* one. Examples: |
| 1178 | // short __attribute__((common)) var; -> declspec |
| 1179 | // short var __attribute__((common)); -> declarator |
| 1180 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1181 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1182 | |
| 1183 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1184 | if (!D.isInvalidType()) { |
| 1185 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1186 | D.complete(ThisDecl); |
| 1187 | if (ThisDecl) |
| 1188 | DeclsInGroup.push_back(ThisDecl); |
| 1189 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | if (DeclEnd) |
| 1193 | *DeclEnd = Tok.getLocation(); |
| 1194 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1195 | if (ExpectSemi && |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1196 | ExpectAndConsume(tok::semi, |
| 1197 | Context == Declarator::FileContext |
| 1198 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1199 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1200 | // Okay, there was no semicolon and one was expected. If we see a |
| 1201 | // declaration specifier, just assume it was missing and continue parsing. |
| 1202 | // Otherwise things are very confused and we skip to recover. |
| 1203 | if (!isDeclarationSpecifier()) { |
| 1204 | SkipUntil(tok::r_brace, true, true); |
| 1205 | if (Tok.is(tok::semi)) |
| 1206 | ConsumeToken(); |
| 1207 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1210 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1211 | DeclsInGroup.data(), |
| 1212 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1215 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1216 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1217 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1218 | // If a simple-asm-expr is present, parse it. |
| 1219 | if (Tok.is(tok::kw_asm)) { |
| 1220 | SourceLocation Loc; |
| 1221 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1222 | if (AsmLabel.isInvalid()) { |
| 1223 | SkipUntil(tok::semi, true, true); |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
| 1227 | D.setAsmLabel(AsmLabel.release()); |
| 1228 | D.SetRangeEnd(Loc); |
| 1229 | } |
| 1230 | |
| 1231 | MaybeParseGNUAttributes(D); |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1235 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1236 | /// declarator'. This method parses the remainder of the declaration |
| 1237 | /// (including any attributes or initializer, among other things) and |
| 1238 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1239 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1240 | /// init-declarator: [C99 6.7] |
| 1241 | /// declarator |
| 1242 | /// declarator '=' initializer |
| 1243 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1244 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1245 | /// [C++] declarator initializer[opt] |
| 1246 | /// |
| 1247 | /// [C++] initializer: |
| 1248 | /// [C++] '=' initializer-clause |
| 1249 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1250 | /// [C++0x] '=' 'default' [TODO] |
| 1251 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1252 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1253 | /// |
| 1254 | /// According to the standard grammar, =default and =delete are function |
| 1255 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1257 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1258 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1259 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1260 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1262 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1265 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1266 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1267 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1268 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1269 | switch (TemplateInfo.Kind) { |
| 1270 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1271 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1272 | break; |
| 1273 | |
| 1274 | case ParsedTemplateInfo::Template: |
| 1275 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1276 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1277 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1278 | TemplateInfo.TemplateParams->data(), |
| 1279 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1280 | D); |
| 1281 | break; |
| 1282 | |
| 1283 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1284 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1285 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1286 | TemplateInfo.ExternLoc, |
| 1287 | TemplateInfo.TemplateLoc, |
| 1288 | D); |
| 1289 | if (ThisRes.isInvalid()) { |
| 1290 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1291 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | ThisDecl = ThisRes.get(); |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1299 | bool TypeContainsAuto = |
| 1300 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1301 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1302 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1303 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1304 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1305 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1306 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1307 | if (D.isFunctionDeclarator()) |
| 1308 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1309 | << 1 /* delete */; |
| 1310 | else |
| 1311 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1312 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1313 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1314 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1315 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1316 | else |
| 1317 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1318 | } else { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1319 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1320 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1321 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1322 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1323 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1324 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1325 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1326 | cutOffParsing(); |
| 1327 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1330 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1331 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1332 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1333 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1334 | ExitScope(); |
| 1335 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1336 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1337 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1338 | SkipUntil(tok::comma, true, true); |
| 1339 | Actions.ActOnInitializerError(ThisDecl); |
| 1340 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1341 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1342 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1343 | } |
| 1344 | } else if (Tok.is(tok::l_paren)) { |
| 1345 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1346 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1347 | T.consumeOpen(); |
| 1348 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1349 | ExprVector Exprs(Actions); |
| 1350 | CommaLocsTy CommaLocs; |
| 1351 | |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1352 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1353 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1354 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1357 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1358 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1361 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1362 | ExitScope(); |
| 1363 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1364 | } else { |
| 1365 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1366 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1367 | |
| 1368 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1369 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1370 | |
| 1371 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1372 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1373 | ExitScope(); |
| 1374 | } |
| 1375 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1376 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1377 | T.getCloseLocation(), |
| 1378 | move_arg(Exprs)); |
| 1379 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1380 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1381 | } |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1382 | } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 1383 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1384 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1385 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1386 | if (D.getCXXScopeSpec().isSet()) { |
| 1387 | EnterScope(0); |
| 1388 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1389 | } |
| 1390 | |
| 1391 | ExprResult Init(ParseBraceInitializer()); |
| 1392 | |
| 1393 | if (D.getCXXScopeSpec().isSet()) { |
| 1394 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1395 | ExitScope(); |
| 1396 | } |
| 1397 | |
| 1398 | if (Init.isInvalid()) { |
| 1399 | Actions.ActOnInitializerError(ThisDecl); |
| 1400 | } else |
| 1401 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1402 | /*DirectInit=*/true, TypeContainsAuto); |
| 1403 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1404 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1405 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1408 | Actions.FinalizeDeclaration(ThisDecl); |
| 1409 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1410 | return ThisDecl; |
| 1411 | } |
| 1412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1413 | /// ParseSpecifierQualifierList |
| 1414 | /// specifier-qualifier-list: |
| 1415 | /// type-specifier specifier-qualifier-list[opt] |
| 1416 | /// type-qualifier specifier-qualifier-list[opt] |
| 1417 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1418 | /// |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1419 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1420 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1421 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1422 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1423 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | // Validate declspec for type-name. |
| 1426 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1427 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1428 | !DS.hasAttributes()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1429 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1431 | // Issue diagnostic and remove storage class if present. |
| 1432 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1433 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1434 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1435 | else |
| 1436 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1437 | DS.ClearStorageClassSpecs(); |
| 1438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1440 | // Issue diagnostic and remove function specfier if present. |
| 1441 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1442 | if (DS.isInlineSpecified()) |
| 1443 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1444 | if (DS.isVirtualSpecified()) |
| 1445 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1446 | if (DS.isExplicitSpecified()) |
| 1447 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1448 | DS.ClearFunctionSpecs(); |
| 1449 | } |
| 1450 | } |
| 1451 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1452 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1453 | /// specified token is valid after the identifier in a declarator which |
| 1454 | /// immediately follows the declspec. For example, these things are valid: |
| 1455 | /// |
| 1456 | /// int x [ 4]; // direct-declarator |
| 1457 | /// int x ( int y); // direct-declarator |
| 1458 | /// int(int x ) // direct-declarator |
| 1459 | /// int x ; // simple-declaration |
| 1460 | /// int x = 17; // init-declarator-list |
| 1461 | /// int x , y; // init-declarator-list |
| 1462 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1463 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1464 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1465 | /// |
| 1466 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1467 | /// ')' happens to be valid anyway). |
| 1468 | /// int (x) |
| 1469 | /// |
| 1470 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1471 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1472 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1473 | 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] | 1474 | } |
| 1475 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1476 | |
| 1477 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1478 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1479 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1480 | /// malformed or is "implicit int" (in K&R and C89). |
| 1481 | /// |
| 1482 | /// This method handles diagnosing this prettily and returns false if the |
| 1483 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1484 | /// other pieces of declspec after it, it returns true. |
| 1485 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1486 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1487 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1488 | AccessSpecifier AS) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1489 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1491 | SourceLocation Loc = Tok.getLocation(); |
| 1492 | // If we see an identifier that is not a type name, we normally would |
| 1493 | // parse it as the identifer being declared. However, when a typename |
| 1494 | // is typo'd or the definition is not included, this will incorrectly |
| 1495 | // parse the typename as the identifier name and fall over misparsing |
| 1496 | // later parts of the diagnostic. |
| 1497 | // |
| 1498 | // As such, we try to do some look-ahead in cases where this would |
| 1499 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1500 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1501 | // an identifier with implicit int, we'd get a parse error because the |
| 1502 | // next token is obviously invalid for a type. Parse these as a case |
| 1503 | // with an invalid type specifier. |
| 1504 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1506 | // Since we know that this either implicit int (which is rare) or an |
| 1507 | // error, we'd do lookahead to try to do better recovery. |
| 1508 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 1509 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1510 | // we just avoid eating the identifier, so it will be parsed as the |
| 1511 | // identifier in the declarator. |
| 1512 | return false; |
| 1513 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1515 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1516 | // error anyway. Try to recover from various common problems. Check |
| 1517 | // to see if this was a reference to a tag name without a tag specified. |
| 1518 | // 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] | 1519 | // |
| 1520 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1521 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1522 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1523 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1525 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1526 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1527 | case DeclSpec::TST_enum: |
| 1528 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1529 | case DeclSpec::TST_union: |
| 1530 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1531 | case DeclSpec::TST_struct: |
| 1532 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1533 | case DeclSpec::TST_class: |
| 1534 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1535 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1537 | if (TagName) { |
| 1538 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
John McCall | 23e907a | 2010-02-14 01:03:10 +0000 | [diff] [blame] | 1539 | << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1540 | << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1542 | // Parse this as a tag as if the missing tag were present. |
| 1543 | if (TagKind == tok::kw_enum) |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1544 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1545 | else |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1546 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1547 | return true; |
| 1548 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1551 | // This is almost certainly an invalid type name. Let the action emit a |
| 1552 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1553 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1554 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1555 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1556 | // The action emitted a diagnostic, so we don't have to. |
| 1557 | if (T) { |
| 1558 | // The action has suggested that the type T could be used. Set that as |
| 1559 | // the type in the declaration specifiers, consume the would-be type |
| 1560 | // name token, and we're done. |
| 1561 | const char *PrevSpec; |
| 1562 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1563 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1564 | DS.SetRangeEnd(Tok.getLocation()); |
| 1565 | ConsumeToken(); |
| 1566 | |
| 1567 | // There may be other declaration specifiers after this. |
| 1568 | return true; |
| 1569 | } |
| 1570 | |
| 1571 | // Fall through; the action had no suggestion for us. |
| 1572 | } else { |
| 1573 | // The action did not emit a diagnostic, so emit one now. |
| 1574 | SourceRange R; |
| 1575 | if (SS) R = SS->getRange(); |
| 1576 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1579 | // Mark this as an error. |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1580 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1581 | unsigned DiagID; |
| 1582 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1583 | DS.SetRangeEnd(Tok.getLocation()); |
| 1584 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1586 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1587 | // avoid rippling error messages on subsequent uses of the same type, |
| 1588 | // could be useful if #include was forgotten. |
| 1589 | return false; |
| 1590 | } |
| 1591 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1592 | /// \brief Determine the declaration specifier context from the declarator |
| 1593 | /// context. |
| 1594 | /// |
| 1595 | /// \param Context the declarator context, which is one of the |
| 1596 | /// Declarator::TheContext enumerator values. |
| 1597 | Parser::DeclSpecContext |
| 1598 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1599 | if (Context == Declarator::MemberContext) |
| 1600 | return DSC_class; |
| 1601 | if (Context == Declarator::FileContext) |
| 1602 | return DSC_top_level; |
| 1603 | return DSC_normal; |
| 1604 | } |
| 1605 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1606 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1607 | /// |
| 1608 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1609 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1610 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1611 | /// [C11] type-id |
| 1612 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1613 | /// [C++0x] type-id ...[opt] |
| 1614 | /// [C++0x] assignment-expression ...[opt] |
| 1615 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1616 | SourceLocation &EllipsisLoc) { |
| 1617 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1618 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1619 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1620 | ParsedType Ty = ParseTypeName().get(); |
| 1621 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1622 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1623 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1624 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1625 | ER = ParseConstantExpression(); |
| 1626 | |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1627 | if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis)) |
| 1628 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1629 | |
| 1630 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1634 | /// attribute to Attrs. |
| 1635 | /// |
| 1636 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1637 | /// [C11] '_Alignas' '(' type-id ')' |
| 1638 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1639 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1640 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1641 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1642 | SourceLocation *endLoc) { |
| 1643 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1644 | "Not an alignment-specifier!"); |
| 1645 | |
| 1646 | SourceLocation KWLoc = Tok.getLocation(); |
| 1647 | ConsumeToken(); |
| 1648 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1649 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1650 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1651 | return; |
| 1652 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1653 | SourceLocation EllipsisLoc; |
| 1654 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1655 | if (ArgExpr.isInvalid()) { |
| 1656 | SkipUntil(tok::r_paren); |
| 1657 | return; |
| 1658 | } |
| 1659 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1660 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1661 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1662 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1663 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1664 | // FIXME: Handle pack-expansions here. |
| 1665 | if (EllipsisLoc.isValid()) { |
| 1666 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1667 | return; |
| 1668 | } |
| 1669 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1670 | ExprVector ArgExprs(Actions); |
| 1671 | ArgExprs.push_back(ArgExpr.release()); |
| 1672 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1673 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1676 | /// ParseDeclarationSpecifiers |
| 1677 | /// declaration-specifiers: [C99 6.7] |
| 1678 | /// storage-class-specifier declaration-specifiers[opt] |
| 1679 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1680 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1681 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1682 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1683 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | /// |
| 1685 | /// storage-class-specifier: [C99 6.7.1] |
| 1686 | /// 'typedef' |
| 1687 | /// 'extern' |
| 1688 | /// 'static' |
| 1689 | /// 'auto' |
| 1690 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1691 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1692 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1693 | /// function-specifier: [C99 6.7.4] |
| 1694 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1695 | /// [C++] 'virtual' |
| 1696 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1697 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1698 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1699 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1700 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1701 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1702 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1703 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1704 | AccessSpecifier AS, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1705 | DeclSpecContext DSContext) { |
| 1706 | if (DS.getSourceRange().isInvalid()) { |
| 1707 | DS.SetRangeStart(Tok.getLocation()); |
| 1708 | DS.SetRangeEnd(Tok.getLocation()); |
| 1709 | } |
| 1710 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1711 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1712 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1713 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1715 | unsigned DiagID = 0; |
| 1716 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1717 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1718 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1719 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1721 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1722 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1723 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1724 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1725 | // If this is not a declaration specifier token, we're done reading decl |
| 1726 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1727 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1728 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1730 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1731 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1732 | if (DS.hasTypeSpecifier()) { |
| 1733 | bool AllowNonIdentifiers |
| 1734 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1735 | Scope::BlockScope | |
| 1736 | Scope::TemplateParamScope | |
| 1737 | Scope::FunctionPrototypeScope | |
| 1738 | Scope::AtCatchScope)) == 0; |
| 1739 | bool AllowNestedNameSpecifiers |
| 1740 | = DSContext == DSC_top_level || |
| 1741 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1742 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1743 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1744 | AllowNonIdentifiers, |
| 1745 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1746 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1749 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1750 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1751 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1752 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1753 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1754 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1755 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1756 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1757 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1758 | |
| 1759 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1760 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1763 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1764 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1765 | if (TryAnnotateCXXScopeToken(true)) { |
| 1766 | if (!DS.hasTypeSpecifier()) |
| 1767 | DS.SetTypeSpecError(); |
| 1768 | goto DoneWithDeclSpec; |
| 1769 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1770 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1771 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1772 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1773 | |
| 1774 | case tok::annot_cxxscope: { |
| 1775 | if (DS.hasTypeSpecifier()) |
| 1776 | goto DoneWithDeclSpec; |
| 1777 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1778 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1779 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1780 | Tok.getAnnotationRange(), |
| 1781 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1782 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1783 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1784 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1786 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1787 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1788 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1789 | |
| 1790 | // C++ [class.qual]p2: |
| 1791 | // In a lookup in which the constructor is an acceptable lookup |
| 1792 | // result and the nested-name-specifier nominates a class C: |
| 1793 | // |
| 1794 | // - if the name specified after the |
| 1795 | // nested-name-specifier, when looked up in C, is the |
| 1796 | // injected-class-name of C (Clause 9), or |
| 1797 | // |
| 1798 | // - if the name specified after the nested-name-specifier |
| 1799 | // is the same as the identifier or the |
| 1800 | // simple-template-id's template-name in the last |
| 1801 | // component of the nested-name-specifier, |
| 1802 | // |
| 1803 | // the name is instead considered to name the constructor of |
| 1804 | // class C. |
| 1805 | // |
| 1806 | // Thus, if the template-name is actually the constructor |
| 1807 | // name, then the code is ill-formed; this interpretation is |
| 1808 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1809 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1810 | if ((DSContext == DSC_top_level || |
| 1811 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1812 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1813 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1814 | if (isConstructorDeclarator()) { |
| 1815 | // The user meant this to be an out-of-line constructor |
| 1816 | // definition, but template arguments are not allowed |
| 1817 | // there. Just allow this as a constructor; we'll |
| 1818 | // complain about it later. |
| 1819 | goto DoneWithDeclSpec; |
| 1820 | } |
| 1821 | |
| 1822 | // The user meant this to name a type, but it actually names |
| 1823 | // a constructor with some extraneous template |
| 1824 | // arguments. Complain, then parse it as a type as the user |
| 1825 | // intended. |
| 1826 | Diag(TemplateId->TemplateNameLoc, |
| 1827 | diag::err_out_of_line_template_id_names_constructor) |
| 1828 | << TemplateId->Name; |
| 1829 | } |
| 1830 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1831 | DS.getTypeSpecScope() = SS; |
| 1832 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1834 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1835 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1836 | continue; |
| 1837 | } |
| 1838 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1839 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1840 | DS.getTypeSpecScope() = SS; |
| 1841 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1842 | if (Tok.getAnnotationValue()) { |
| 1843 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1844 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1845 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1846 | PrevSpec, DiagID, T); |
| 1847 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1848 | else |
| 1849 | DS.SetTypeSpecError(); |
| 1850 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1851 | ConsumeToken(); // The typename |
| 1852 | } |
| 1853 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1854 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1855 | goto DoneWithDeclSpec; |
| 1856 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1857 | // If we're in a context where the identifier could be a class name, |
| 1858 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1859 | if ((DSContext == DSC_top_level || |
| 1860 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1861 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1862 | &SS)) { |
| 1863 | if (isConstructorDeclarator()) |
| 1864 | goto DoneWithDeclSpec; |
| 1865 | |
| 1866 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1867 | // of the class is qualified in a context where it could name |
| 1868 | // a constructor, its a constructor name. However, we've |
| 1869 | // looked at the declarator, and the user probably meant this |
| 1870 | // to be a type. Complain that it isn't supposed to be treated |
| 1871 | // as a type, then proceed to parse it as a type. |
| 1872 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1873 | << Next.getIdentifierInfo(); |
| 1874 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1875 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1876 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1877 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1878 | getCurScope(), &SS, |
| 1879 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 1880 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1881 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1882 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1883 | // If the referenced identifier is not a type, then this declspec is |
| 1884 | // erroneous: We already checked about that it has no type specifier, and |
| 1885 | // 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] | 1886 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1887 | if (TypeRep == 0) { |
| 1888 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1889 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1890 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1893 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1894 | ConsumeToken(); // The C++ scope. |
| 1895 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1896 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1897 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1898 | if (isInvalid) |
| 1899 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1901 | DS.SetRangeEnd(Tok.getLocation()); |
| 1902 | ConsumeToken(); // The typename. |
| 1903 | |
| 1904 | continue; |
| 1905 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1907 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1908 | if (Tok.getAnnotationValue()) { |
| 1909 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 1910 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1911 | DiagID, T); |
| 1912 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1913 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1914 | |
| 1915 | if (isInvalid) |
| 1916 | break; |
| 1917 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1918 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1919 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1921 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1922 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1923 | // Objective-C interface. |
| 1924 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1925 | ParseObjCProtocolQualifiers(DS); |
| 1926 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1927 | continue; |
| 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 1930 | case tok::kw___is_signed: |
| 1931 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 1932 | // typically treats it as a trait. If we see __is_signed as it appears |
| 1933 | // in libstdc++, e.g., |
| 1934 | // |
| 1935 | // static const bool __is_signed; |
| 1936 | // |
| 1937 | // then treat __is_signed as an identifier rather than as a keyword. |
| 1938 | if (DS.getTypeSpecType() == TST_bool && |
| 1939 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 1940 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1941 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 1942 | Tok.setKind(tok::identifier); |
| 1943 | } |
| 1944 | |
| 1945 | // We're done with the declaration-specifiers. |
| 1946 | goto DoneWithDeclSpec; |
| 1947 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1948 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1949 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1950 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1951 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1952 | // so handle it as such. This is important for ctor parsing. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1953 | if (getLang().CPlusPlus) { |
| 1954 | if (TryAnnotateCXXScopeToken(true)) { |
| 1955 | if (!DS.hasTypeSpecifier()) |
| 1956 | DS.SetTypeSpecError(); |
| 1957 | goto DoneWithDeclSpec; |
| 1958 | } |
| 1959 | if (!Tok.is(tok::identifier)) |
| 1960 | continue; |
| 1961 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1963 | // This identifier can only be a typedef name if we haven't already seen |
| 1964 | // a type-specifier. Without this check we misparse: |
| 1965 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1966 | if (DS.hasTypeSpecifier()) |
| 1967 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1969 | // Check for need to substitute AltiVec keyword tokens. |
| 1970 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1971 | break; |
| 1972 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1973 | // It has to be available as a typedef too! |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1974 | ParsedType TypeRep = |
| 1975 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1976 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1977 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1978 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1979 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1980 | if (!TypeRep) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1981 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1982 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1983 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1984 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1985 | // If we're in a context where the identifier could be a class name, |
| 1986 | // check whether this is a constructor declaration. |
| 1987 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1988 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1989 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1990 | goto DoneWithDeclSpec; |
| 1991 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1992 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1993 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1994 | if (isInvalid) |
| 1995 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1997 | DS.SetRangeEnd(Tok.getLocation()); |
| 1998 | ConsumeToken(); // The identifier |
| 1999 | |
| 2000 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2001 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2002 | // Objective-C interface. |
| 2003 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 2004 | ParseObjCProtocolQualifiers(DS); |
| 2005 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2006 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2007 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2008 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2009 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2010 | |
| 2011 | // type-name |
| 2012 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2013 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2014 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2015 | // This template-id does not refer to a type name, so we're |
| 2016 | // done with the type-specifiers. |
| 2017 | goto DoneWithDeclSpec; |
| 2018 | } |
| 2019 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2020 | // If we're in a context where the template-id could be a |
| 2021 | // constructor name or specialization, check whether this is a |
| 2022 | // constructor declaration. |
| 2023 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2024 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2025 | isConstructorDeclarator()) |
| 2026 | goto DoneWithDeclSpec; |
| 2027 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2028 | // Turn the template-id annotation token into a type annotation |
| 2029 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2030 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2031 | continue; |
| 2032 | } |
| 2033 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2034 | // GNU attributes support. |
| 2035 | case tok::kw___attribute: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2036 | ParseGNUAttributes(DS.getAttributes()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2037 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2038 | |
| 2039 | // Microsoft declspec support. |
| 2040 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2041 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2042 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2044 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2045 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2046 | // FIXME: Add handling here! |
| 2047 | break; |
| 2048 | |
| 2049 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2050 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2051 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2052 | case tok::kw___cdecl: |
| 2053 | case tok::kw___stdcall: |
| 2054 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2055 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2056 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2057 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2058 | continue; |
| 2059 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2060 | // Borland single token adornments. |
| 2061 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2062 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2063 | continue; |
| 2064 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2065 | // OpenCL single token adornments. |
| 2066 | case tok::kw___kernel: |
| 2067 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2068 | continue; |
| 2069 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2070 | // storage-class-specifier |
| 2071 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2072 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2073 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | case tok::kw_extern: |
| 2076 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2077 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2078 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2079 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2080 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2081 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2082 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2083 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2084 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | case tok::kw_static: |
| 2086 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2087 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2088 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2089 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2090 | break; |
| 2091 | case tok::kw_auto: |
Douglas Gregor | 18d8b79 | 2011-03-14 21:43:30 +0000 | [diff] [blame] | 2092 | if (getLang().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2093 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2094 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2095 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2096 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2097 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2098 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2099 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2100 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2101 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2102 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2103 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2104 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2105 | break; |
| 2106 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2107 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2108 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2110 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2111 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2112 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2113 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2115 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2116 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2118 | // function-specifier |
| 2119 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2120 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2121 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2122 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2123 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2124 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2125 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2126 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2127 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2128 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2129 | // alignment-specifier |
| 2130 | case tok::kw__Alignas: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2131 | if (!getLang().C11) |
| 2132 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2133 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2134 | continue; |
| 2135 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2136 | // friend |
| 2137 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2138 | if (DSContext == DSC_class) |
| 2139 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2140 | else { |
| 2141 | PrevSpec = ""; // not actually used by the diagnostic |
| 2142 | DiagID = diag::err_friend_invalid_in_context; |
| 2143 | isInvalid = true; |
| 2144 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2145 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2147 | // Modules |
| 2148 | case tok::kw___module_private__: |
| 2149 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2150 | break; |
| 2151 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2152 | // constexpr |
| 2153 | case tok::kw_constexpr: |
| 2154 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2155 | break; |
| 2156 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2157 | // type-specifier |
| 2158 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2159 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2160 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2161 | break; |
| 2162 | case tok::kw_long: |
| 2163 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2164 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2165 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2166 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2167 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2168 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2169 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2170 | case tok::kw___int64: |
| 2171 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2172 | DiagID); |
| 2173 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2174 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2175 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2176 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2177 | break; |
| 2178 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2179 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2180 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2181 | break; |
| 2182 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2183 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2184 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2185 | break; |
| 2186 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2187 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2188 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2189 | break; |
| 2190 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2191 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2192 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2193 | break; |
| 2194 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2195 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2196 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2197 | break; |
| 2198 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2199 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2200 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2201 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2202 | case tok::kw_half: |
| 2203 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2204 | DiagID); |
| 2205 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2206 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2207 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2208 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2209 | break; |
| 2210 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2211 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2212 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2213 | break; |
| 2214 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2215 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2216 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2217 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2218 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2219 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2220 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2221 | break; |
| 2222 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2223 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2224 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2225 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2226 | case tok::kw_bool: |
| 2227 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2228 | if (Tok.is(tok::kw_bool) && |
| 2229 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2230 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2231 | PrevSpec = ""; // Not used by the diagnostic. |
| 2232 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2233 | // For better error recovery. |
| 2234 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2235 | isInvalid = true; |
| 2236 | } else { |
| 2237 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2238 | DiagID); |
| 2239 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2242 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2243 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2244 | break; |
| 2245 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2246 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2247 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2248 | break; |
| 2249 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2250 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2251 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2252 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2253 | case tok::kw___vector: |
| 2254 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2255 | break; |
| 2256 | case tok::kw___pixel: |
| 2257 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2258 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2259 | case tok::kw___unknown_anytype: |
| 2260 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2261 | PrevSpec, DiagID); |
| 2262 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2263 | |
| 2264 | // class-specifier: |
| 2265 | case tok::kw_class: |
| 2266 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2267 | case tok::kw_union: { |
| 2268 | tok::TokenKind Kind = Tok.getKind(); |
| 2269 | ConsumeToken(); |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2270 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2271 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2272 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2273 | |
| 2274 | // enum-specifier: |
| 2275 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2276 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2277 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2278 | continue; |
| 2279 | |
| 2280 | // cv-qualifier: |
| 2281 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2282 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
| 2283 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2284 | break; |
| 2285 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2286 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 2287 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2288 | break; |
| 2289 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2290 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 2291 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2294 | // C++ typename-specifier: |
| 2295 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2296 | if (TryAnnotateTypeOrScopeToken()) { |
| 2297 | DS.SetTypeSpecError(); |
| 2298 | goto DoneWithDeclSpec; |
| 2299 | } |
| 2300 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2301 | continue; |
| 2302 | break; |
| 2303 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2304 | // GNU typeof support. |
| 2305 | case tok::kw_typeof: |
| 2306 | ParseTypeofSpecifier(DS); |
| 2307 | continue; |
| 2308 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2309 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2310 | ParseDecltypeSpecifier(DS); |
| 2311 | continue; |
| 2312 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2313 | case tok::kw___underlying_type: |
| 2314 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2315 | continue; |
| 2316 | |
| 2317 | case tok::kw__Atomic: |
| 2318 | ParseAtomicSpecifier(DS); |
| 2319 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2320 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2321 | // OpenCL qualifiers: |
| 2322 | case tok::kw_private: |
| 2323 | if (!getLang().OpenCL) |
| 2324 | goto DoneWithDeclSpec; |
| 2325 | case tok::kw___private: |
| 2326 | case tok::kw___global: |
| 2327 | case tok::kw___local: |
| 2328 | case tok::kw___constant: |
| 2329 | case tok::kw___read_only: |
| 2330 | case tok::kw___write_only: |
| 2331 | case tok::kw___read_write: |
| 2332 | ParseOpenCLQualifiers(DS); |
| 2333 | break; |
| 2334 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2335 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2336 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2337 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2338 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2339 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2340 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2342 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2343 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2344 | << FixItHint::CreateInsertion(Loc, "id") |
| 2345 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2346 | |
| 2347 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2348 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2349 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2350 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2351 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2352 | if (isInvalid) { |
| 2353 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2354 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2355 | |
| 2356 | if (DiagID == diag::ext_duplicate_declspec) |
| 2357 | Diag(Tok, DiagID) |
| 2358 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2359 | else |
| 2360 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2361 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2362 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2363 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2364 | if (DiagID != diag::err_bool_redeclaration) |
| 2365 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2366 | } |
| 2367 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2368 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2369 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2370 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 2371 | /// which together subsume the C grammar. Note that the C++ |
| 2372 | /// type-specifier also includes the C type-qualifier (for const, |
| 2373 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 2374 | /// found (and parsed), false otherwise. |
| 2375 | /// |
| 2376 | /// type-specifier: [C++ 7.1.5] |
| 2377 | /// simple-type-specifier |
| 2378 | /// class-specifier |
| 2379 | /// enum-specifier |
| 2380 | /// elaborated-type-specifier [TODO] |
| 2381 | /// cv-qualifier |
| 2382 | /// |
| 2383 | /// cv-qualifier: [C++ 7.1.5.1] |
| 2384 | /// 'const' |
| 2385 | /// 'volatile' |
| 2386 | /// [C99] 'restrict' |
| 2387 | /// |
| 2388 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 2389 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 2390 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 2391 | /// 'char' |
| 2392 | /// 'wchar_t' |
| 2393 | /// 'bool' |
| 2394 | /// 'short' |
| 2395 | /// 'int' |
| 2396 | /// 'long' |
| 2397 | /// 'signed' |
| 2398 | /// 'unsigned' |
| 2399 | /// 'float' |
| 2400 | /// 'double' |
| 2401 | /// 'void' |
| 2402 | /// [C99] '_Bool' |
| 2403 | /// [C99] '_Complex' |
| 2404 | /// [C99] '_Imaginary' // Removed in TC2? |
| 2405 | /// [GNU] '_Decimal32' |
| 2406 | /// [GNU] '_Decimal64' |
| 2407 | /// [GNU] '_Decimal128' |
| 2408 | /// [GNU] typeof-specifier |
| 2409 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 2410 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2411 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2412 | /// [AltiVec] '__vector' |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2413 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2414 | const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2415 | unsigned &DiagID, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2416 | const ParsedTemplateInfo &TemplateInfo, |
| 2417 | bool SuppressDeclarations) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2418 | SourceLocation Loc = Tok.getLocation(); |
| 2419 | |
| 2420 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2421 | case tok::identifier: // foo::bar |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 2422 | // If we already have a type specifier, this identifier is not a type. |
| 2423 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 2424 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 2425 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 2426 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2427 | // Check for need to substitute AltiVec keyword tokens. |
| 2428 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2429 | break; |
| 2430 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2431 | case tok::kw_decltype: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2432 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2433 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2434 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2435 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2436 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2437 | return true; |
| 2438 | if (Tok.is(tok::identifier)) |
| 2439 | return false; |
| 2440 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2441 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2442 | case tok::coloncolon: // ::foo::bar |
| 2443 | if (NextToken().is(tok::kw_new) || // ::new |
| 2444 | NextToken().is(tok::kw_delete)) // ::delete |
| 2445 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2447 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2448 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2449 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2450 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2451 | return true; |
| 2452 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2453 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2455 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2456 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2457 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2458 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 2459 | Tok.getAnnotationEndLoc(), PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2460 | DiagID, T); |
| 2461 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2462 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2463 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2464 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2466 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2467 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 2468 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 2469 | // just a normal reference to a typedef name. |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2470 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 2471 | ParseObjCProtocolQualifiers(DS); |
| 2472 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2473 | return true; |
| 2474 | } |
| 2475 | |
| 2476 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2477 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | case tok::kw_long: |
| 2480 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2481 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2482 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2483 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2484 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2485 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2486 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2487 | case tok::kw___int64: |
| 2488 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2489 | DiagID); |
| 2490 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2491 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2492 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2493 | break; |
| 2494 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2495 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2496 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2497 | break; |
| 2498 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2499 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2500 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2501 | break; |
| 2502 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2503 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2504 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2505 | break; |
| 2506 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2507 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2510 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2511 | break; |
| 2512 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2513 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2514 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2515 | case tok::kw_half: |
| 2516 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID); |
| 2517 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2518 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2519 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2520 | break; |
| 2521 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2522 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2523 | break; |
| 2524 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2525 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2526 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2527 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2528 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2531 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2532 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2533 | case tok::kw_bool: |
| 2534 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2535 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2536 | break; |
| 2537 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2538 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2539 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2540 | break; |
| 2541 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2542 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2543 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2544 | break; |
| 2545 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2546 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2547 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2548 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2549 | case tok::kw___vector: |
| 2550 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2551 | break; |
| 2552 | case tok::kw___pixel: |
| 2553 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2554 | break; |
| 2555 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2556 | // class-specifier: |
| 2557 | case tok::kw_class: |
| 2558 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2559 | case tok::kw_union: { |
| 2560 | tok::TokenKind Kind = Tok.getKind(); |
| 2561 | ConsumeToken(); |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2562 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2563 | /*EnteringContext=*/false, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2564 | SuppressDeclarations); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2565 | return true; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2566 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2567 | |
| 2568 | // enum-specifier: |
| 2569 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2570 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2571 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2572 | return true; |
| 2573 | |
| 2574 | // cv-qualifier: |
| 2575 | case tok::kw_const: |
| 2576 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2577 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | case tok::kw_volatile: |
| 2580 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2581 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2582 | break; |
| 2583 | case tok::kw_restrict: |
| 2584 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2585 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2586 | break; |
| 2587 | |
| 2588 | // GNU typeof support. |
| 2589 | case tok::kw_typeof: |
| 2590 | ParseTypeofSpecifier(DS); |
| 2591 | return true; |
| 2592 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2593 | // C++0x decltype support. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2594 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2595 | ParseDecltypeSpecifier(DS); |
| 2596 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2598 | // C++0x type traits support. |
| 2599 | case tok::kw___underlying_type: |
| 2600 | ParseUnderlyingTypeSpecifier(DS); |
| 2601 | return true; |
| 2602 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2603 | case tok::kw__Atomic: |
| 2604 | ParseAtomicSpecifier(DS); |
| 2605 | return true; |
| 2606 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2607 | // OpenCL qualifiers: |
| 2608 | case tok::kw_private: |
| 2609 | if (!getLang().OpenCL) |
| 2610 | return false; |
| 2611 | case tok::kw___private: |
| 2612 | case tok::kw___global: |
| 2613 | case tok::kw___local: |
| 2614 | case tok::kw___constant: |
| 2615 | case tok::kw___read_only: |
| 2616 | case tok::kw___write_only: |
| 2617 | case tok::kw___read_write: |
| 2618 | ParseOpenCLQualifiers(DS); |
| 2619 | break; |
| 2620 | |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2621 | // C++0x auto support. |
| 2622 | case tok::kw_auto: |
Richard Smith | 87e96eb | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 2623 | // This is only called in situations where a storage-class specifier is |
| 2624 | // illegal, so we can assume an auto type specifier was intended even in |
| 2625 | // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate |
| 2626 | // extension diagnostic. |
| 2627 | if (!getLang().CPlusPlus) |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2628 | return false; |
| 2629 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2630 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2631 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2632 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2633 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2634 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2635 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2636 | case tok::kw___cdecl: |
| 2637 | case tok::kw___stdcall: |
| 2638 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2639 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2640 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2641 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 2642 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2643 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2644 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2645 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2646 | return true; |
| 2647 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2648 | default: |
| 2649 | // Not a type-specifier; do nothing. |
| 2650 | return false; |
| 2651 | } |
| 2652 | |
| 2653 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2654 | if (isInvalid) { |
| 2655 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2656 | // Pick between error or extwarn. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2657 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2658 | } |
| 2659 | DS.SetRangeEnd(Tok.getLocation()); |
| 2660 | ConsumeToken(); // whatever we parsed above. |
| 2661 | return true; |
| 2662 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2664 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2665 | /// semicolon. |
| 2666 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2667 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2668 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2669 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2670 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2671 | /// struct-declarator-list: |
| 2672 | /// struct-declarator |
| 2673 | /// struct-declarator-list ',' struct-declarator |
| 2674 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2675 | /// struct-declarator: |
| 2676 | /// declarator |
| 2677 | /// [GNU] declarator attributes[opt] |
| 2678 | /// declarator[opt] ':' constant-expression |
| 2679 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2680 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2681 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2682 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2683 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2684 | if (Tok.is(tok::kw___extension__)) { |
| 2685 | // __extension__ silences extension warnings in the subexpression. |
| 2686 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2687 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2688 | return ParseStructDeclaration(DS, Fields); |
| 2689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2691 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2692 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2694 | // If there are no declarators, this is a free-standing declaration |
| 2695 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2696 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2697 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | |
| 2701 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2702 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2703 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2704 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2705 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2706 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2707 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2708 | |
| 2709 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2710 | if (!FirstDeclarator) |
| 2711 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2713 | /// struct-declarator: declarator |
| 2714 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2715 | if (Tok.isNot(tok::colon)) { |
| 2716 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2717 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2718 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2721 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2722 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2723 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2724 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2725 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2726 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2727 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2728 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2729 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2730 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2731 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2732 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2733 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2734 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2735 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2736 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2737 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2738 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2739 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2740 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2741 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2742 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2743 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2744 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2745 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2746 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | /// ParseStructUnionBody |
| 2750 | /// struct-contents: |
| 2751 | /// struct-declaration-list |
| 2752 | /// [EXT] empty |
| 2753 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2754 | /// struct-declaration-list: |
| 2755 | /// struct-declaration |
| 2756 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2757 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2758 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2759 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2760 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2761 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2762 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2764 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2765 | if (T.consumeOpen()) |
| 2766 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2768 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2769 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2770 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2771 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2772 | // C++. |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2773 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) { |
| 2774 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2775 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2776 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2777 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2778 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2779 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2780 | // 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] | 2781 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2782 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2783 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2785 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2786 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2787 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2788 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2789 | ConsumeToken(); |
| 2790 | continue; |
| 2791 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2792 | |
| 2793 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2794 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2796 | if (!Tok.is(tok::at)) { |
| 2797 | struct CFieldCallback : FieldCallback { |
| 2798 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2799 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2800 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2801 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2802 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2803 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2804 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2805 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2806 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2807 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2808 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2809 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2810 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2811 | FieldDecls.push_back(Field); |
| 2812 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2813 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2814 | } Callback(*this, TagDecl, FieldDecls); |
| 2815 | |
| 2816 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2817 | } else { // Handle @defs |
| 2818 | ConsumeToken(); |
| 2819 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2820 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2821 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2822 | continue; |
| 2823 | } |
| 2824 | ConsumeToken(); |
| 2825 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2826 | if (!Tok.is(tok::identifier)) { |
| 2827 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2828 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2829 | continue; |
| 2830 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2831 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2832 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2833 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2834 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2835 | ConsumeToken(); |
| 2836 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2839 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2840 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2841 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2842 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2843 | break; |
| 2844 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2845 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2846 | // 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] | 2847 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2848 | // If we stopped at a ';', eat it. |
| 2849 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2850 | } |
| 2851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2853 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2855 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2857 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2858 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2859 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2860 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2861 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2862 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2863 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2864 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2865 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2868 | /// ParseEnumSpecifier |
| 2869 | /// enum-specifier: [C99 6.7.2.2] |
| 2870 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2871 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2872 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2873 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame^] | 2874 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2875 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2876 | /// 'enum' identifier |
| 2877 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2878 | /// |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2879 | /// [C++0x] enum-head '{' enumerator-list[opt] '}' |
| 2880 | /// [C++0x] enum-head '{' enumerator-list ',' '}' |
| 2881 | /// |
| 2882 | /// enum-head: [C++0x] |
| 2883 | /// enum-key attributes[opt] identifier[opt] enum-base[opt] |
| 2884 | /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt] |
| 2885 | /// |
| 2886 | /// enum-key: [C++0x] |
| 2887 | /// 'enum' |
| 2888 | /// 'enum' 'class' |
| 2889 | /// 'enum' 'struct' |
| 2890 | /// |
| 2891 | /// enum-base: [C++0x] |
| 2892 | /// ':' type-specifier-seq |
| 2893 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2894 | /// [C++] elaborated-type-specifier: |
| 2895 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2896 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2897 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2898 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2899 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2900 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2901 | if (Tok.is(tok::code_completion)) { |
| 2902 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2903 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2904 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2905 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2906 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2907 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2908 | bool IsScopedUsingClassTag = false; |
| 2909 | |
| 2910 | if (getLang().CPlusPlus0x && |
| 2911 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2912 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2913 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2914 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2915 | } |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2916 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2917 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2918 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2919 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2920 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame^] | 2921 | // If declspecs exist after tag, parse them. |
| 2922 | while (Tok.is(tok::kw___declspec)) |
| 2923 | ParseMicrosoftDeclSpec(attrs); |
| 2924 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 2925 | bool AllowFixedUnderlyingType |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2926 | = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2927 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2928 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2929 | if (getLang().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2930 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2931 | // if a fixed underlying type is allowed. |
| 2932 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2933 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2934 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2935 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2936 | return; |
| 2937 | |
| 2938 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2939 | Diag(Tok, diag::err_expected_ident); |
| 2940 | if (Tok.isNot(tok::l_brace)) { |
| 2941 | // Has no name and is not a definition. |
| 2942 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2943 | SkipUntil(tok::comma, true); |
| 2944 | return; |
| 2945 | } |
| 2946 | } |
| 2947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2949 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2950 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
| 2951 | (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2952 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2954 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2955 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2956 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2959 | // If an identifier is present, consume and remember it. |
| 2960 | IdentifierInfo *Name = 0; |
| 2961 | SourceLocation NameLoc; |
| 2962 | if (Tok.is(tok::identifier)) { |
| 2963 | Name = Tok.getIdentifierInfo(); |
| 2964 | NameLoc = ConsumeToken(); |
| 2965 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2967 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2968 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2969 | // declaration of a scoped enumeration. |
| 2970 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2971 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2972 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | TypeResult BaseType; |
| 2976 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2977 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2978 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2979 | bool PossibleBitfield = false; |
| 2980 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2981 | // If we're in class scope, this can either be an enum declaration with |
| 2982 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2983 | // use a simple disambiguation scheme first to catch the common cases |
| 2984 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2985 | // anything that's a simple-type-specifier followed by '(' as an |
| 2986 | // expression. This suffices because function types are not valid |
| 2987 | // underlying types anyway. |
| 2988 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2989 | // If the next token starts an expression, we know we're parsing a |
| 2990 | // bit-field. This is the common case. |
| 2991 | if (TPR == TPResult::True()) |
| 2992 | PossibleBitfield = true; |
| 2993 | // If the next token starts a type-specifier-seq, it may be either a |
| 2994 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2995 | // lookahead one more token to see if it's obvious that we have a |
| 2996 | // fixed underlying type. |
| 2997 | else if (TPR == TPResult::False() && |
| 2998 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2999 | // Consume the ':'. |
| 3000 | ConsumeToken(); |
| 3001 | } else { |
| 3002 | // We have the start of a type-specifier-seq, so we have to perform |
| 3003 | // tentative parsing to determine whether we have an expression or a |
| 3004 | // type. |
| 3005 | TentativeParsingAction TPA(*this); |
| 3006 | |
| 3007 | // Consume the ':'. |
| 3008 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3009 | |
| 3010 | // If we see a type specifier followed by an open-brace, we have an |
| 3011 | // ambiguity between an underlying type and a C++11 braced |
| 3012 | // function-style cast. Resolve this by always treating it as an |
| 3013 | // underlying type. |
| 3014 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3015 | // this case. |
| 3016 | if ((getLang().CPlusPlus && |
| 3017 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3018 | (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3019 | // We'll parse this as a bitfield later. |
| 3020 | PossibleBitfield = true; |
| 3021 | TPA.Revert(); |
| 3022 | } else { |
| 3023 | // We have a type-specifier-seq. |
| 3024 | TPA.Commit(); |
| 3025 | } |
| 3026 | } |
| 3027 | } else { |
| 3028 | // Consume the ':'. |
| 3029 | ConsumeToken(); |
| 3030 | } |
| 3031 | |
| 3032 | if (!PossibleBitfield) { |
| 3033 | SourceRange Range; |
| 3034 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3035 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 3036 | if (!getLang().CPlusPlus0x && !getLang().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3037 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 3038 | << Range; |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3039 | if (getLang().CPlusPlus0x) |
| 3040 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3041 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3044 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3045 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3046 | // 'enum foo;', then this is a forward declaration. If we have |
| 3047 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3048 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3049 | // |
| 3050 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3051 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3052 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3053 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3054 | Sema::TagUseKind TUK; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3055 | if (DS.isFriendSpecified()) |
| 3056 | TUK = Sema::TUK_Friend; |
| 3057 | else if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3058 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3059 | else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3060 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3061 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3062 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3063 | |
| 3064 | // enums cannot be templates, although they can be referenced from a |
| 3065 | // template. |
| 3066 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3067 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3068 | Diag(Tok, diag::err_enum_template); |
| 3069 | |
| 3070 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3071 | SkipUntil(tok::comma, true); |
| 3072 | return; |
| 3073 | } |
| 3074 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3075 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3076 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
| 3077 | |
| 3078 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3079 | SkipUntil(tok::comma, true); |
| 3080 | return; |
| 3081 | } |
| 3082 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3083 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3084 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3085 | const char *PrevSpec = 0; |
| 3086 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3087 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3088 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 3089 | AS, DS.getModulePrivateSpecLoc(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3090 | MultiTemplateParamsArg(Actions), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3091 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3092 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3094 | if (IsDependent) { |
| 3095 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 3096 | // dependent tag. |
| 3097 | if (!Name) { |
| 3098 | DS.SetTypeSpecError(); |
| 3099 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3100 | return; |
| 3101 | } |
| 3102 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3103 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3104 | TUK, SS, Name, StartLoc, |
| 3105 | NameLoc); |
| 3106 | if (Type.isInvalid()) { |
| 3107 | DS.SetTypeSpecError(); |
| 3108 | return; |
| 3109 | } |
| 3110 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3111 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3112 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3113 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3114 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3115 | |
| 3116 | return; |
| 3117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3119 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3120 | // The action failed to produce an enumeration tag. If this is a |
| 3121 | // definition, consume the entire definition. |
| 3122 | if (Tok.is(tok::l_brace)) { |
| 3123 | ConsumeBrace(); |
| 3124 | SkipUntil(tok::r_brace); |
| 3125 | } |
| 3126 | |
| 3127 | DS.SetTypeSpecError(); |
| 3128 | return; |
| 3129 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3130 | |
| 3131 | if (Tok.is(tok::l_brace)) { |
| 3132 | if (TUK == Sema::TUK_Friend) |
| 3133 | Diag(Tok, diag::err_friend_decl_defines_type) |
| 3134 | << SourceRange(DS.getFriendSpecLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3135 | ParseEnumBody(StartLoc, TagDecl); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3136 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3138 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3139 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3140 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3141 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3145 | /// enumerator-list: |
| 3146 | /// enumerator |
| 3147 | /// enumerator-list ',' enumerator |
| 3148 | /// enumerator: |
| 3149 | /// enumeration-constant |
| 3150 | /// enumeration-constant '=' constant-expression |
| 3151 | /// enumeration-constant: |
| 3152 | /// identifier |
| 3153 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3154 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3155 | // Enter the scope of the enum body and start the definition. |
| 3156 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3157 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3159 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3160 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3162 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3163 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3164 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3166 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3167 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3168 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3170 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3171 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3172 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3173 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3175 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3176 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3177 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3179 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3180 | ExprResult AssignedVal; |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3181 | ParsingDeclRAIIObject PD(*this); |
| 3182 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3183 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3184 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3185 | AssignedVal = ParseConstantExpression(); |
| 3186 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3187 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3190 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3191 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3192 | LastEnumConstDecl, |
| 3193 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3194 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3195 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3196 | PD.complete(EnumConstDecl); |
| 3197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3198 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3199 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3201 | if (Tok.is(tok::identifier)) { |
| 3202 | // We're missing a comma between enumerators. |
| 3203 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3204 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3205 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3206 | continue; |
| 3207 | } |
| 3208 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3209 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3210 | break; |
| 3211 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3213 | if (Tok.isNot(tok::identifier)) { |
| 3214 | if (!getLang().C99 && !getLang().CPlusPlus0x) |
| 3215 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 3216 | << getLang().CPlusPlus |
| 3217 | << FixItHint::CreateRemoval(CommaLoc); |
| 3218 | else if (getLang().CPlusPlus0x) |
| 3219 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3220 | << FixItHint::CreateRemoval(CommaLoc); |
| 3221 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3222 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3224 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3225 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3227 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3228 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3229 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3231 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3232 | EnumDecl, EnumConstantDecls.data(), |
| 3233 | EnumConstantDecls.size(), getCurScope(), |
| 3234 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3236 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3237 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3238 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3242 | /// start of a type-qualifier-list. |
| 3243 | bool Parser::isTypeQualifier() const { |
| 3244 | switch (Tok.getKind()) { |
| 3245 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3246 | |
| 3247 | // type-qualifier only in OpenCL |
| 3248 | case tok::kw_private: |
| 3249 | return getLang().OpenCL; |
| 3250 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3251 | // type-qualifier |
| 3252 | case tok::kw_const: |
| 3253 | case tok::kw_volatile: |
| 3254 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3255 | case tok::kw___private: |
| 3256 | case tok::kw___local: |
| 3257 | case tok::kw___global: |
| 3258 | case tok::kw___constant: |
| 3259 | case tok::kw___read_only: |
| 3260 | case tok::kw___read_write: |
| 3261 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3262 | return true; |
| 3263 | } |
| 3264 | } |
| 3265 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3266 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3267 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3268 | /// specifier or if we're not sure. |
| 3269 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3270 | switch (Tok.getKind()) { |
| 3271 | default: return false; |
| 3272 | // type-specifiers |
| 3273 | case tok::kw_short: |
| 3274 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3275 | case tok::kw___int64: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3276 | case tok::kw_signed: |
| 3277 | case tok::kw_unsigned: |
| 3278 | case tok::kw__Complex: |
| 3279 | case tok::kw__Imaginary: |
| 3280 | case tok::kw_void: |
| 3281 | case tok::kw_char: |
| 3282 | case tok::kw_wchar_t: |
| 3283 | case tok::kw_char16_t: |
| 3284 | case tok::kw_char32_t: |
| 3285 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3286 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3287 | case tok::kw_float: |
| 3288 | case tok::kw_double: |
| 3289 | case tok::kw_bool: |
| 3290 | case tok::kw__Bool: |
| 3291 | case tok::kw__Decimal32: |
| 3292 | case tok::kw__Decimal64: |
| 3293 | case tok::kw__Decimal128: |
| 3294 | case tok::kw___vector: |
| 3295 | |
| 3296 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3297 | case tok::kw_class: |
| 3298 | case tok::kw_struct: |
| 3299 | case tok::kw_union: |
| 3300 | // enum-specifier |
| 3301 | case tok::kw_enum: |
| 3302 | |
| 3303 | // typedef-name |
| 3304 | case tok::annot_typename: |
| 3305 | return true; |
| 3306 | } |
| 3307 | } |
| 3308 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3309 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3310 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3311 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3312 | switch (Tok.getKind()) { |
| 3313 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3315 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3316 | if (TryAltiVecVectorToken()) |
| 3317 | return true; |
| 3318 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3319 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3320 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3321 | // recurse to handle whatever we get. |
| 3322 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3323 | return true; |
| 3324 | if (Tok.is(tok::identifier)) |
| 3325 | return false; |
| 3326 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3327 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3328 | case tok::coloncolon: // ::foo::bar |
| 3329 | if (NextToken().is(tok::kw_new) || // ::new |
| 3330 | NextToken().is(tok::kw_delete)) // ::delete |
| 3331 | return false; |
| 3332 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3333 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3334 | return true; |
| 3335 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3337 | // GNU attributes support. |
| 3338 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3339 | // GNU typeof support. |
| 3340 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3342 | // type-specifiers |
| 3343 | case tok::kw_short: |
| 3344 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3345 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3346 | case tok::kw_signed: |
| 3347 | case tok::kw_unsigned: |
| 3348 | case tok::kw__Complex: |
| 3349 | case tok::kw__Imaginary: |
| 3350 | case tok::kw_void: |
| 3351 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3352 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3353 | case tok::kw_char16_t: |
| 3354 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3355 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3356 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3357 | case tok::kw_float: |
| 3358 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3359 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3360 | case tok::kw__Bool: |
| 3361 | case tok::kw__Decimal32: |
| 3362 | case tok::kw__Decimal64: |
| 3363 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3364 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3366 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3367 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3368 | case tok::kw_struct: |
| 3369 | case tok::kw_union: |
| 3370 | // enum-specifier |
| 3371 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3373 | // type-qualifier |
| 3374 | case tok::kw_const: |
| 3375 | case tok::kw_volatile: |
| 3376 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3377 | |
| 3378 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3379 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3380 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3382 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3383 | case tok::less: |
| 3384 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3386 | case tok::kw___cdecl: |
| 3387 | case tok::kw___stdcall: |
| 3388 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3389 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3390 | case tok::kw___w64: |
| 3391 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3392 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3393 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3394 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3395 | |
| 3396 | case tok::kw___private: |
| 3397 | case tok::kw___local: |
| 3398 | case tok::kw___global: |
| 3399 | case tok::kw___constant: |
| 3400 | case tok::kw___read_only: |
| 3401 | case tok::kw___read_write: |
| 3402 | case tok::kw___write_only: |
| 3403 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3404 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3405 | |
| 3406 | case tok::kw_private: |
| 3407 | return getLang().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3408 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3409 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3410 | case tok::kw__Atomic: |
| 3411 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3416 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3417 | /// |
| 3418 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3419 | /// this check is to disambiguate between an expression and a declaration. |
| 3420 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3421 | switch (Tok.getKind()) { |
| 3422 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3424 | case tok::kw_private: |
| 3425 | return getLang().OpenCL; |
| 3426 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3427 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3428 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 3429 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 3430 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3431 | if (TryAltiVecVectorToken()) |
| 3432 | return true; |
| 3433 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3434 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3435 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3436 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3437 | // recurse to handle whatever we get. |
| 3438 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3439 | return true; |
| 3440 | if (Tok.is(tok::identifier)) |
| 3441 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3442 | |
| 3443 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3444 | // by an identifier and then either ':' or ']', in a place where an |
| 3445 | // expression is permitted, then this is probably a class message send |
| 3446 | // missing the initial '['. In this case, we won't consider this to be |
| 3447 | // the start of a declaration. |
| 3448 | if (DisambiguatingWithExpression && |
| 3449 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3450 | return false; |
| 3451 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3452 | return isDeclarationSpecifier(); |
| 3453 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3454 | case tok::coloncolon: // ::foo::bar |
| 3455 | if (NextToken().is(tok::kw_new) || // ::new |
| 3456 | NextToken().is(tok::kw_delete)) // ::delete |
| 3457 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3459 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3460 | // recurse to handle whatever we get. |
| 3461 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3462 | return true; |
| 3463 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3465 | // storage-class-specifier |
| 3466 | case tok::kw_typedef: |
| 3467 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3468 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3469 | case tok::kw_static: |
| 3470 | case tok::kw_auto: |
| 3471 | case tok::kw_register: |
| 3472 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3474 | // Modules |
| 3475 | case tok::kw___module_private__: |
| 3476 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3477 | // type-specifiers |
| 3478 | case tok::kw_short: |
| 3479 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3480 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3481 | case tok::kw_signed: |
| 3482 | case tok::kw_unsigned: |
| 3483 | case tok::kw__Complex: |
| 3484 | case tok::kw__Imaginary: |
| 3485 | case tok::kw_void: |
| 3486 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3487 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3488 | case tok::kw_char16_t: |
| 3489 | case tok::kw_char32_t: |
| 3490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3491 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3492 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3493 | case tok::kw_float: |
| 3494 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3495 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3496 | case tok::kw__Bool: |
| 3497 | case tok::kw__Decimal32: |
| 3498 | case tok::kw__Decimal64: |
| 3499 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3500 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3502 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3503 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3504 | case tok::kw_struct: |
| 3505 | case tok::kw_union: |
| 3506 | // enum-specifier |
| 3507 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3509 | // type-qualifier |
| 3510 | case tok::kw_const: |
| 3511 | case tok::kw_volatile: |
| 3512 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3514 | // function-specifier |
| 3515 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3516 | case tok::kw_virtual: |
| 3517 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3518 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3519 | // static_assert-declaration |
| 3520 | case tok::kw__Static_assert: |
| 3521 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3522 | // GNU typeof support. |
| 3523 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3525 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3526 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3527 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3529 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3530 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3531 | return true; |
| 3532 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3533 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3534 | case tok::kw__Atomic: |
| 3535 | return true; |
| 3536 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3537 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3538 | case tok::less: |
| 3539 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3541 | // typedef-name |
| 3542 | case tok::annot_typename: |
| 3543 | return !DisambiguatingWithExpression || |
| 3544 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3545 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3546 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3547 | case tok::kw___cdecl: |
| 3548 | case tok::kw___stdcall: |
| 3549 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3550 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3551 | case tok::kw___w64: |
| 3552 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3553 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3554 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3555 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3556 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3557 | |
| 3558 | case tok::kw___private: |
| 3559 | case tok::kw___local: |
| 3560 | case tok::kw___global: |
| 3561 | case tok::kw___constant: |
| 3562 | case tok::kw___read_only: |
| 3563 | case tok::kw___read_write: |
| 3564 | case tok::kw___write_only: |
| 3565 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3566 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3570 | bool Parser::isConstructorDeclarator() { |
| 3571 | TentativeParsingAction TPA(*this); |
| 3572 | |
| 3573 | // Parse the C++ scope specifier. |
| 3574 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3575 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3576 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3577 | TPA.Revert(); |
| 3578 | return false; |
| 3579 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3580 | |
| 3581 | // Parse the constructor name. |
| 3582 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3583 | // We already know that we have a constructor name; just consume |
| 3584 | // the token. |
| 3585 | ConsumeToken(); |
| 3586 | } else { |
| 3587 | TPA.Revert(); |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
| 3591 | // Current class name must be followed by a left parentheses. |
| 3592 | if (Tok.isNot(tok::l_paren)) { |
| 3593 | TPA.Revert(); |
| 3594 | return false; |
| 3595 | } |
| 3596 | ConsumeParen(); |
| 3597 | |
| 3598 | // A right parentheses or ellipsis signals that we have a constructor. |
| 3599 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 3600 | TPA.Revert(); |
| 3601 | return true; |
| 3602 | } |
| 3603 | |
| 3604 | // If we need to, enter the specified scope. |
| 3605 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3606 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3607 | DeclScopeObj.EnterDeclaratorScope(); |
| 3608 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3609 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3610 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3611 | MaybeParseMicrosoftAttributes(Attrs); |
| 3612 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3613 | // Check whether the next token(s) are part of a declaration |
| 3614 | // specifier, in which case we have the start of a parameter and, |
| 3615 | // therefore, we know that this is a constructor. |
| 3616 | bool IsConstructor = isDeclarationSpecifier(); |
| 3617 | TPA.Revert(); |
| 3618 | return IsConstructor; |
| 3619 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3620 | |
| 3621 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3622 | /// type-qualifier-list: [C99 6.7.5] |
| 3623 | /// type-qualifier |
| 3624 | /// [vendor] attributes |
| 3625 | /// [ only if VendorAttributesAllowed=true ] |
| 3626 | /// type-qualifier-list type-qualifier |
| 3627 | /// [vendor] type-qualifier-list attributes |
| 3628 | /// [ only if VendorAttributesAllowed=true ] |
| 3629 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3630 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3631 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3632 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3633 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3634 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3635 | bool CXX0XAttributesAllowed) { |
| 3636 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3637 | SourceLocation Loc = Tok.getLocation(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3638 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3639 | ParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3640 | if (CXX0XAttributesAllowed) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3641 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3642 | else |
| 3643 | Diag(Loc, diag::err_attributes_not_allowed); |
| 3644 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3645 | |
| 3646 | SourceLocation EndLoc; |
| 3647 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3648 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3649 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3650 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3651 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3652 | SourceLocation Loc = Tok.getLocation(); |
| 3653 | |
| 3654 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3655 | case tok::code_completion: |
| 3656 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3657 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3658 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3659 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3660 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
| 3661 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3662 | break; |
| 3663 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3664 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 3665 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3666 | break; |
| 3667 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3668 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 3669 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3670 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3671 | |
| 3672 | // OpenCL qualifiers: |
| 3673 | case tok::kw_private: |
| 3674 | if (!getLang().OpenCL) |
| 3675 | goto DoneWithTypeQuals; |
| 3676 | case tok::kw___private: |
| 3677 | case tok::kw___global: |
| 3678 | case tok::kw___local: |
| 3679 | case tok::kw___constant: |
| 3680 | case tok::kw___read_only: |
| 3681 | case tok::kw___write_only: |
| 3682 | case tok::kw___read_write: |
| 3683 | ParseOpenCLQualifiers(DS); |
| 3684 | break; |
| 3685 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3686 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3687 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3688 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3689 | case tok::kw___cdecl: |
| 3690 | case tok::kw___stdcall: |
| 3691 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3692 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3693 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3694 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3695 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3696 | continue; |
| 3697 | } |
| 3698 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3699 | case tok::kw___pascal: |
| 3700 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3701 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3702 | continue; |
| 3703 | } |
| 3704 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3705 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3706 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3707 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3708 | continue; // do *not* consume the next token! |
| 3709 | } |
| 3710 | // otherwise, FALL THROUGH! |
| 3711 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3712 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3713 | // If this is not a type-qualifier token, we're done reading type |
| 3714 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3715 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3716 | if (EndLoc.isValid()) |
| 3717 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3718 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3719 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3721 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3722 | if (isInvalid) { |
| 3723 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3724 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3725 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3726 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3732 | /// |
| 3733 | void Parser::ParseDeclarator(Declarator &D) { |
| 3734 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3735 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3736 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3739 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3740 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3741 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3742 | /// ptr-operator production. |
| 3743 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3744 | /// If the grammar of this construct is extended, matching changes must also be |
| 3745 | /// made to TryParseDeclarator and MightBeDeclarator. |
| 3746 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3747 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3748 | /// [C] pointer[opt] direct-declarator |
| 3749 | /// [C++] direct-declarator |
| 3750 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3751 | /// |
| 3752 | /// pointer: [C99 6.7.5] |
| 3753 | /// '*' type-qualifier-list[opt] |
| 3754 | /// '*' type-qualifier-list[opt] pointer |
| 3755 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3756 | /// ptr-operator: |
| 3757 | /// '*' cv-qualifier-seq[opt] |
| 3758 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3759 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3760 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3761 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3762 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3763 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3764 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3765 | if (Diags.hasAllExtensionsSilenced()) |
| 3766 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3767 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3768 | // C++ member pointers start with a '::' or a nested-name. |
| 3769 | // Member pointers get special handling, since there's no place for the |
| 3770 | // scope spec in the generic path below. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3771 | if (getLang().CPlusPlus && |
| 3772 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3773 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3774 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3775 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3776 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3777 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3778 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3779 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3780 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3781 | // The scope spec really belongs to the direct-declarator. |
| 3782 | D.getCXXScopeSpec() = SS; |
| 3783 | if (DirectDeclParser) |
| 3784 | (this->*DirectDeclParser)(D); |
| 3785 | return; |
| 3786 | } |
| 3787 | |
| 3788 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3789 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3790 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3791 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3792 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Recurse to parse whatever is left. |
| 3795 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3796 | |
| 3797 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3798 | // scope. It has to catch pointers into namespace scope anyway. |
| 3799 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3800 | Loc), |
| 3801 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3802 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3803 | return; |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3808 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3809 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3810 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3811 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3812 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3813 | if (DirectDeclParser) |
| 3814 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3815 | return; |
| 3816 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3817 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3818 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3819 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3820 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3821 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3822 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3823 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3824 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3825 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3826 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3827 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3828 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3829 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3830 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3831 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3832 | if (Kind == tok::star) |
| 3833 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3834 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3835 | DS.getConstSpecLoc(), |
| 3836 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3837 | DS.getRestrictSpecLoc()), |
| 3838 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3839 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3840 | else |
| 3841 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3843 | Loc), |
| 3844 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3845 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3846 | } else { |
| 3847 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3848 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3849 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3850 | // Complain about rvalue references in C++03, but then go on and build |
| 3851 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3852 | if (Kind == tok::ampamp) |
| 3853 | Diag(Loc, getLang().CPlusPlus0x ? |
| 3854 | diag::warn_cxx98_compat_rvalue_reference : |
| 3855 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3856 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3857 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3858 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3859 | // template type argument, in which case the cv-qualifiers are ignored. |
| 3860 | // |
| 3861 | // [GNU] Retricted references are allowed. |
| 3862 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3863 | // [C++0x] Attributes on references are not allowed. |
| 3864 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3865 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3866 | |
| 3867 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3868 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3869 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3870 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3871 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3872 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3873 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3877 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3879 | if (D.getNumTypeObjects() > 0) { |
| 3880 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3881 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3882 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3883 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3884 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3885 | << II; |
| 3886 | else |
| 3887 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3888 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3889 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3890 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3891 | // can go ahead and build the (technically ill-formed) |
| 3892 | // declarator: reference collapsing will take care of it. |
| 3893 | } |
| 3894 | } |
| 3895 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3896 | // 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] | 3897 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3898 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3899 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3900 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | /// ParseDirectDeclarator |
| 3905 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3906 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3907 | /// '(' declarator ')' |
| 3908 | /// [GNU] '(' attributes declarator ')' |
| 3909 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3910 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3911 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3912 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3913 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3914 | /// direct-declarator '(' parameter-type-list ')' |
| 3915 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3916 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3917 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3918 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3919 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3920 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3921 | /// |
| 3922 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3923 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3924 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3925 | /// |
| 3926 | /// id-expression: [C++ 5.1] |
| 3927 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3928 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3929 | /// |
| 3930 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3932 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3933 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3935 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3936 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3937 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3938 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3939 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3940 | if (getLang().CPlusPlus && D.mayHaveIdentifier()) { |
| 3941 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3942 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3943 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3944 | D.getContext() == Declarator::MemberContext; |
| 3945 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3946 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3949 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3950 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3951 | // Change the declaration context for name lookup, until this function |
| 3952 | // is exited (and the declarator has been parsed). |
| 3953 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3956 | // C++0x [dcl.fct]p14: |
| 3957 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3958 | // of a parameter-declaration-clause without a preceding comma. In |
| 3959 | // this case, the ellipsis is parsed as part of the |
| 3960 | // abstract-declarator if the type of the parameter names a template |
| 3961 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3962 | // as part of the parameter-declaration-clause. |
| 3963 | if (Tok.is(tok::ellipsis) && |
| 3964 | !((D.getContext() == Declarator::PrototypeContext || |
| 3965 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3966 | NextToken().is(tok::r_paren) && |
| 3967 | !Actions.containsUnexpandedParameterPacks(D))) |
| 3968 | D.setEllipsisLoc(ConsumeToken()); |
| 3969 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3970 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3971 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3972 | // We found something that indicates the start of an unqualified-id. |
| 3973 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3974 | bool AllowConstructorName; |
| 3975 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3976 | AllowConstructorName = false; |
| 3977 | else if (D.getCXXScopeSpec().isSet()) |
| 3978 | AllowConstructorName = |
| 3979 | (D.getContext() == Declarator::FileContext || |
| 3980 | (D.getContext() == Declarator::MemberContext && |
| 3981 | D.getDeclSpec().isFriendSpecified())); |
| 3982 | else |
| 3983 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3984 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3985 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3986 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3987 | /*EnteringContext=*/true, |
| 3988 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3989 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3990 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3991 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3992 | D.getName()) || |
| 3993 | // Once we're past the identifier, if the scope was bad, mark the |
| 3994 | // whole declarator bad. |
| 3995 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3996 | D.SetIdentifier(0, Tok.getLocation()); |
| 3997 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3998 | } else { |
| 3999 | // Parsed the unqualified-id; update range information and move along. |
| 4000 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4001 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4002 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4003 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4004 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4005 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4006 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4007 | assert(!getLang().CPlusPlus && |
| 4008 | "There's a C++-specific check for tok::identifier above"); |
| 4009 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4010 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4011 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4012 | goto PastIdentifier; |
| 4013 | } |
| 4014 | |
| 4015 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4016 | // direct-declarator: '(' declarator ')' |
| 4017 | // direct-declarator: '(' attributes declarator ')' |
| 4018 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4019 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4020 | |
| 4021 | // If the declarator was parenthesized, we entered the declarator |
| 4022 | // scope when parsing the parenthesized declarator, then exited |
| 4023 | // the scope already. Re-enter the scope, if we need to. |
| 4024 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4025 | // If there was an error parsing parenthesized declarator, declarator |
| 4026 | // scope may have been enterred before. Don't do it again. |
| 4027 | if (!D.isInvalidType() && |
| 4028 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4029 | // Change the declaration context for name lookup, until this function |
| 4030 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4031 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4032 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4033 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4034 | // This could be something simple like "int" (in which case the declarator |
| 4035 | // portion is empty), if an abstract-declarator is allowed. |
| 4036 | D.SetIdentifier(0, Tok.getLocation()); |
| 4037 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4038 | if (D.getContext() == Declarator::MemberContext) |
| 4039 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4040 | << D.getDeclSpec().getSourceRange(); |
| 4041 | else if (getLang().CPlusPlus) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 4042 | Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4043 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4044 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4045 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4046 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4047 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4049 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4050 | assert(D.isPastIdentifier() && |
| 4051 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4052 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4053 | // Don't parse attributes unless we have an identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4054 | if (D.getIdentifier()) |
| 4055 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4056 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4057 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4058 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4059 | // Enter function-declaration scope, limiting any declarators to the |
| 4060 | // function prototype scope, including parameter declarators. |
| 4061 | ParseScope PrototypeScope(this, |
| 4062 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4063 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4064 | // In such a case, check if we actually have a function declarator; if it |
| 4065 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4066 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4067 | // When not in file scope, warn for ambiguous function declarators, just |
| 4068 | // in case the author intended it as a variable definition. |
| 4069 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4070 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4071 | break; |
| 4072 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4073 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4074 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4075 | T.consumeOpen(); |
| 4076 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4077 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4078 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4079 | ParseBracketDeclarator(D); |
| 4080 | } else { |
| 4081 | break; |
| 4082 | } |
| 4083 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4084 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4086 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4087 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4088 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4089 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4090 | /// |
| 4091 | /// direct-declarator: |
| 4092 | /// '(' declarator ')' |
| 4093 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4094 | /// direct-declarator '(' parameter-type-list ')' |
| 4095 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4096 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4097 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4098 | /// |
| 4099 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4100 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4101 | T.consumeOpen(); |
| 4102 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4103 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4105 | // Eat any attributes before we look at whether this is a grouping or function |
| 4106 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4107 | // the type being built up, for example: |
| 4108 | // int (__attribute__(()) *x)(long y) |
| 4109 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4110 | // first argument, for example: |
| 4111 | // int (__attribute__(()) int x) |
| 4112 | // In either case, we need to eat any attributes to be able to determine what |
| 4113 | // sort of paren this is. |
| 4114 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4115 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4116 | bool RequiresArg = false; |
| 4117 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4118 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4120 | // We require that the argument list (if this is a non-grouping paren) be |
| 4121 | // present even if the attribute list was empty. |
| 4122 | RequiresArg = true; |
| 4123 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4124 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4125 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4126 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4127 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4128 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4129 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4130 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4131 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4132 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4133 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4135 | // If we haven't past the identifier yet (or where the identifier would be |
| 4136 | // stored, if this is an abstract declarator), then this is probably just |
| 4137 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4138 | // this could also be the start of function arguments (consider 'void()'). |
| 4139 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4141 | if (!D.mayOmitIdentifier()) { |
| 4142 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4143 | // paren, because we haven't seen the identifier yet. |
| 4144 | isGrouping = true; |
| 4145 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 4146 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4147 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 4148 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4149 | // considered to be a type, not a K&R identifier-list. |
| 4150 | isGrouping = false; |
| 4151 | } else { |
| 4152 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4153 | isGrouping = true; |
| 4154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4155 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4156 | // If this is a grouping paren, handle: |
| 4157 | // direct-declarator: '(' declarator ')' |
| 4158 | // direct-declarator: '(' attributes declarator ')' |
| 4159 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4160 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4161 | D.setGroupingParens(true); |
| 4162 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4163 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4164 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4165 | T.consumeClose(); |
| 4166 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4167 | T.getCloseLocation()), |
| 4168 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4169 | |
| 4170 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4171 | return; |
| 4172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4174 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4175 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4176 | // identifier (and remember where it would have been), then call into |
| 4177 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4178 | D.SetIdentifier(0, Tok.getLocation()); |
| 4179 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4180 | // Enter function-declaration scope, limiting any declarators to the |
| 4181 | // function prototype scope, including parameter declarators. |
| 4182 | ParseScope PrototypeScope(this, |
| 4183 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4184 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4185 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4189 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4190 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4191 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4192 | /// If attrs is non-null, then the caller parsed those arguments immediately |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4193 | /// after the open paren - they should be considered to be the first argument of |
| 4194 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 4195 | /// function is required to be present and required to not be an identifier |
| 4196 | /// list. |
| 4197 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4198 | /// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt], |
| 4199 | /// (C++0x) ref-qualifier[opt], exception-specification[opt], and |
| 4200 | /// (C++0x) trailing-return-type[opt]. |
| 4201 | /// |
| 4202 | /// [C++0x] exception-specification: |
| 4203 | /// dynamic-exception-specification |
| 4204 | /// noexcept-specification |
| 4205 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4206 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4207 | ParsedAttributes &attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4208 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4209 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4210 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4211 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4212 | // lparen is already consumed! |
| 4213 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4214 | |
| 4215 | // This should be true when the function has typed arguments. |
| 4216 | // Otherwise, it is treated as a K&R-style function. |
| 4217 | bool HasProto = false; |
| 4218 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4219 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4220 | // Remember where we see an ellipsis, if any. |
| 4221 | SourceLocation EllipsisLoc; |
| 4222 | |
| 4223 | DeclSpec DS(AttrFactory); |
| 4224 | bool RefQualifierIsLValueRef = true; |
| 4225 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4226 | SourceLocation ConstQualifierLoc; |
| 4227 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4228 | ExceptionSpecificationType ESpecType = EST_None; |
| 4229 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4230 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4231 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4232 | ExprResult NoexceptExpr; |
| 4233 | ParsedType TrailingReturnType; |
| 4234 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4235 | Actions.ActOnStartFunctionDeclarator(); |
| 4236 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4237 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4238 | if (isFunctionDeclaratorIdentifierList()) { |
| 4239 | if (RequiresArg) |
| 4240 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4241 | |
| 4242 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4243 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4244 | Tracker.consumeClose(); |
| 4245 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4246 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4247 | if (Tok.isNot(tok::r_paren)) |
| 4248 | ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc); |
| 4249 | else if (RequiresArg) |
| 4250 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4251 | |
| 4252 | HasProto = ParamInfo.size() || getLang().CPlusPlus; |
| 4253 | |
| 4254 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4255 | Tracker.consumeClose(); |
| 4256 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4257 | |
| 4258 | if (getLang().CPlusPlus) { |
| 4259 | MaybeParseCXX0XAttributes(attrs); |
| 4260 | |
| 4261 | // Parse cv-qualifier-seq[opt]. |
| 4262 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4263 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4264 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4265 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4266 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4267 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4268 | |
| 4269 | // Parse ref-qualifier[opt]. |
| 4270 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4271 | Diag(Tok, getLang().CPlusPlus0x ? |
| 4272 | diag::warn_cxx98_compat_ref_qualifier : |
| 4273 | diag::ext_ref_qualifier); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4274 | |
| 4275 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4276 | RefQualifierLoc = ConsumeToken(); |
| 4277 | EndLoc = RefQualifierLoc; |
| 4278 | } |
| 4279 | |
| 4280 | // Parse exception-specification[opt]. |
| 4281 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 4282 | DynamicExceptions, |
| 4283 | DynamicExceptionRanges, |
| 4284 | NoexceptExpr); |
| 4285 | if (ESpecType != EST_None) |
| 4286 | EndLoc = ESpecRange.getEnd(); |
| 4287 | |
| 4288 | // Parse trailing-return-type[opt]. |
| 4289 | if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4290 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4291 | SourceRange Range; |
| 4292 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4293 | if (Range.getEnd().isValid()) |
| 4294 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | // Remember that we parsed a function type, and remember the attributes. |
| 4300 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4301 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4302 | EllipsisLoc, |
| 4303 | ParamInfo.data(), ParamInfo.size(), |
| 4304 | DS.getTypeQualifiers(), |
| 4305 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4306 | RefQualifierLoc, ConstQualifierLoc, |
| 4307 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4308 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4309 | ESpecType, ESpecRange.getBegin(), |
| 4310 | DynamicExceptions.data(), |
| 4311 | DynamicExceptionRanges.data(), |
| 4312 | DynamicExceptions.size(), |
| 4313 | NoexceptExpr.isUsable() ? |
| 4314 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4315 | Tracker.getOpenLocation(), |
| 4316 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4317 | TrailingReturnType), |
| 4318 | attrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4319 | |
| 4320 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4324 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4325 | /// |
| 4326 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4327 | /// abstract-declarators. |
| 4328 | bool Parser::isFunctionDeclaratorIdentifierList() { |
| 4329 | return !getLang().CPlusPlus |
| 4330 | && Tok.is(tok::identifier) |
| 4331 | && !TryAltiVecVectorToken() |
| 4332 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4333 | // 6.7.5.3p11. |
| 4334 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4335 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4336 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4337 | // identifier lists are really rare in the brave new modern world, and |
| 4338 | // it is very common for someone to typo a type in a non-K&R style |
| 4339 | // list. If we are presented with something like: "void foo(intptr x, |
| 4340 | // float y)", we don't want to start parsing the function declarator as |
| 4341 | // though it is a K&R style declarator just because intptr is an |
| 4342 | // invalid type. |
| 4343 | // |
| 4344 | // To handle this, we check to see if the token after the first |
| 4345 | // identifier is a "," or ")". Only then do we parse it as an |
| 4346 | // identifier list. |
| 4347 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4348 | } |
| 4349 | |
| 4350 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4351 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4352 | /// |
| 4353 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4354 | /// |
| 4355 | /// identifier-list: [C99 6.7.5] |
| 4356 | /// identifier |
| 4357 | /// identifier-list ',' identifier |
| 4358 | /// |
| 4359 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4360 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4361 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4362 | // If there was no identifier specified for the declarator, either we are in |
| 4363 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4364 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4365 | // diagnose this. |
| 4366 | if (!D.getIdentifier()) |
| 4367 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4368 | |
| 4369 | // Maintain an efficient lookup of params we have seen so far. |
| 4370 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4371 | |
| 4372 | while (1) { |
| 4373 | // If this isn't an identifier, report the error and skip until ')'. |
| 4374 | if (Tok.isNot(tok::identifier)) { |
| 4375 | Diag(Tok, diag::err_expected_ident); |
| 4376 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4377 | // Forget we parsed anything. |
| 4378 | ParamInfo.clear(); |
| 4379 | return; |
| 4380 | } |
| 4381 | |
| 4382 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4383 | |
| 4384 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4385 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4386 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4387 | |
| 4388 | // Verify that the argument identifier has not already been mentioned. |
| 4389 | if (!ParamsSoFar.insert(ParmII)) { |
| 4390 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4391 | } else { |
| 4392 | // Remember this identifier in ParamInfo. |
| 4393 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4394 | Tok.getLocation(), |
| 4395 | 0)); |
| 4396 | } |
| 4397 | |
| 4398 | // Eat the identifier. |
| 4399 | ConsumeToken(); |
| 4400 | |
| 4401 | // The list continues if we see a comma. |
| 4402 | if (Tok.isNot(tok::comma)) |
| 4403 | break; |
| 4404 | ConsumeToken(); |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4409 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4410 | /// identifier list. |
| 4411 | /// |
| 4412 | /// D is the declarator being parsed. If attrs is non-null, then the caller |
| 4413 | /// parsed those arguments immediately after the open paren - they should be |
| 4414 | /// considered to be the first argument of a parameter. |
| 4415 | /// |
| 4416 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4417 | /// be the location of the ellipsis, if any was parsed. |
| 4418 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4419 | /// parameter-type-list: [C99 6.7.5] |
| 4420 | /// parameter-list |
| 4421 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4422 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4423 | /// |
| 4424 | /// parameter-list: [C99 6.7.5] |
| 4425 | /// parameter-declaration |
| 4426 | /// parameter-list ',' parameter-declaration |
| 4427 | /// |
| 4428 | /// parameter-declaration: [C99 6.7.5] |
| 4429 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4430 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4431 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4432 | /// declaration-specifiers abstract-declarator[opt] |
| 4433 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4434 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4435 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 4436 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4437 | void Parser::ParseParameterDeclarationClause( |
| 4438 | Declarator &D, |
| 4439 | ParsedAttributes &attrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4440 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4441 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4442 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4443 | while (1) { |
| 4444 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4445 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4446 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4447 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4449 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4450 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4451 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4452 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4453 | // Skip any Microsoft attributes before a param. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4454 | if (getLang().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4455 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4456 | |
| 4457 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4458 | |
| 4459 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4460 | // 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] | 4461 | // FIXME: If we saw an ellipsis first, this code is not reached. Are the |
| 4462 | // attributes lost? Should they even be allowed? |
| 4463 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
| 4464 | // get rid of a parameter (attrs) and this statement. It might be too much |
| 4465 | // hassle. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4466 | DS.takeAttributesFrom(attrs); |
| 4467 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4468 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4470 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4471 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4472 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4473 | ParseDeclarator(ParmDecl); |
| 4474 | |
| 4475 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4476 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4477 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4478 | // Remember this parsed parameter in ParamInfo. |
| 4479 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4481 | // DefArgToks is used when the parsing of default arguments needs |
| 4482 | // to be delayed. |
| 4483 | CachedTokens *DefArgToks = 0; |
| 4484 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4485 | // If no parameter was specified, verify that *something* was specified, |
| 4486 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4487 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4488 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4489 | // Completely missing, emit error. |
| 4490 | Diag(DSStart, diag::err_missing_param); |
| 4491 | } else { |
| 4492 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4493 | // 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] | 4494 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4495 | // Inform the actions module about the parameter declarator, so it gets |
| 4496 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4497 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4498 | |
| 4499 | // Parse the default argument, if any. We parse the default |
| 4500 | // arguments in all dialects; the semantic analysis in |
| 4501 | // ActOnParamDefaultArgument will reject the default argument in |
| 4502 | // C. |
| 4503 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4504 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4505 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4506 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4507 | if (D.getContext() == Declarator::MemberContext) { |
| 4508 | // If we're inside a class definition, cache the tokens |
| 4509 | // corresponding to the default argument. We'll actually parse |
| 4510 | // them when we see the end of the class definition. |
| 4511 | // FIXME: Templates will require something similar. |
| 4512 | // FIXME: Can we use a smart pointer for Toks? |
| 4513 | DefArgToks = new CachedTokens; |
| 4514 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4515 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4516 | /*StopAtSemi=*/true, |
| 4517 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4518 | delete DefArgToks; |
| 4519 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4520 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4521 | } else { |
| 4522 | // Mark the end of the default argument so that we know when to |
| 4523 | // stop when we parse it later on. |
| 4524 | Token DefArgEnd; |
| 4525 | DefArgEnd.startToken(); |
| 4526 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4527 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4528 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4529 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4530 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4531 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4532 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4533 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4534 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4536 | // The argument isn't actually potentially evaluated unless it is |
| 4537 | // used. |
| 4538 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4539 | Sema::PotentiallyEvaluatedIfUsed, |
| 4540 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4541 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4542 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4543 | if (DefArgResult.isInvalid()) { |
| 4544 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4545 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4546 | } else { |
| 4547 | // Inform the actions module about the default argument |
| 4548 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4549 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4550 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4551 | } |
| 4552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4553 | |
| 4554 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4555 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4556 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | // 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] | 4560 | if (Tok.isNot(tok::comma)) { |
| 4561 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4562 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4563 | |
| 4564 | if (!getLang().CPlusPlus) { |
| 4565 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4566 | // in C. Complain and provide the fix. |
| 4567 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4568 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | break; |
| 4573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4574 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4575 | // Consume the comma. |
| 4576 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4578 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4579 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4580 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4581 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4582 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4583 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4584 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4585 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 4586 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4587 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4588 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4590 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4591 | // This code does a fast path to handle some of the most obvious cases. |
| 4592 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4593 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4594 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4595 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4596 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4597 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4598 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4599 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4600 | T.getOpenLocation(), |
| 4601 | T.getCloseLocation()), |
| 4602 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4603 | return; |
| 4604 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4605 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4606 | // [4] is very common. Parse the numeric constant expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4607 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4608 | ConsumeToken(); |
| 4609 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4610 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4611 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4612 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4613 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4614 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4615 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4616 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4617 | T.getOpenLocation(), |
| 4618 | T.getCloseLocation()), |
| 4619 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4620 | return; |
| 4621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4623 | // If valid, this location is the position where we read the 'static' keyword. |
| 4624 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4625 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4626 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4627 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4628 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4629 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4630 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4631 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4633 | // If we haven't already read 'static', check to see if there is one after the |
| 4634 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4635 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4636 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4638 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4639 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4640 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4642 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4643 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4644 | // the the token after the star is a ']'. Since stars in arrays are |
| 4645 | // infrequent, use of lookahead is not costly here. |
| 4646 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4647 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4648 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4649 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4650 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4651 | StaticLoc = SourceLocation(); // Drop the static. |
| 4652 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4653 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4654 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4655 | // Note, in C89, this production uses the constant-expr production instead |
| 4656 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4657 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4658 | // 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] | 4659 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4660 | // Parse the constant-expression or assignment-expression now (depending |
| 4661 | // on dialect). |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4662 | if (getLang().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4663 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4664 | } else { |
| 4665 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4666 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4667 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4668 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4671 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4672 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4673 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4674 | // If the expression was invalid, skip it. |
| 4675 | SkipUntil(tok::r_square); |
| 4676 | return; |
| 4677 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4678 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4679 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4680 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4681 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4682 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4683 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4684 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4685 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4686 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4687 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4688 | T.getOpenLocation(), |
| 4689 | T.getCloseLocation()), |
| 4690 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4693 | /// [GNU] typeof-specifier: |
| 4694 | /// typeof ( expressions ) |
| 4695 | /// typeof ( type-name ) |
| 4696 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4697 | /// |
| 4698 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4699 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4700 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4701 | SourceLocation StartLoc = ConsumeToken(); |
| 4702 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4703 | const bool hasParens = Tok.is(tok::l_paren); |
| 4704 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4705 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4706 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4707 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4708 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4709 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4710 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4711 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4712 | if (hasParens) |
| 4713 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4714 | |
| 4715 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4716 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4717 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4718 | else |
| 4719 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4721 | if (isCastExpr) { |
| 4722 | if (!CastTy) { |
| 4723 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4724 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4725 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4726 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4727 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4728 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4729 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4730 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4731 | DiagID, CastTy)) |
| 4732 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4733 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4734 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4735 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4736 | // If we get here, the operand to the typeof was an expresion. |
| 4737 | if (Operand.isInvalid()) { |
| 4738 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4739 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4740 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4741 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4742 | // We might need to transform the operand if it is potentially evaluated. |
| 4743 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4744 | if (Operand.isInvalid()) { |
| 4745 | DS.SetTypeSpecError(); |
| 4746 | return; |
| 4747 | } |
| 4748 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4749 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4750 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4751 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4752 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4753 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4754 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4755 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4756 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4757 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4758 | /// _Atomic ( type-name ) |
| 4759 | /// |
| 4760 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4761 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4762 | |
| 4763 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4764 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4765 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4766 | SkipUntil(tok::r_paren); |
| 4767 | return; |
| 4768 | } |
| 4769 | |
| 4770 | TypeResult Result = ParseTypeName(); |
| 4771 | if (Result.isInvalid()) { |
| 4772 | SkipUntil(tok::r_paren); |
| 4773 | return; |
| 4774 | } |
| 4775 | |
| 4776 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4777 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4779 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4780 | return; |
| 4781 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4782 | DS.setTypeofParensRange(T.getRange()); |
| 4783 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4784 | |
| 4785 | const char *PrevSpec = 0; |
| 4786 | unsigned DiagID; |
| 4787 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4788 | DiagID, Result.release())) |
| 4789 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4790 | } |
| 4791 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4792 | |
| 4793 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4794 | /// from TryAltiVecVectorToken. |
| 4795 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4796 | Token Next = NextToken(); |
| 4797 | switch (Next.getKind()) { |
| 4798 | default: return false; |
| 4799 | case tok::kw_short: |
| 4800 | case tok::kw_long: |
| 4801 | case tok::kw_signed: |
| 4802 | case tok::kw_unsigned: |
| 4803 | case tok::kw_void: |
| 4804 | case tok::kw_char: |
| 4805 | case tok::kw_int: |
| 4806 | case tok::kw_float: |
| 4807 | case tok::kw_double: |
| 4808 | case tok::kw_bool: |
| 4809 | case tok::kw___pixel: |
| 4810 | Tok.setKind(tok::kw___vector); |
| 4811 | return true; |
| 4812 | case tok::identifier: |
| 4813 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4814 | Tok.setKind(tok::kw___vector); |
| 4815 | return true; |
| 4816 | } |
| 4817 | return false; |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4822 | const char *&PrevSpec, unsigned &DiagID, |
| 4823 | bool &isInvalid) { |
| 4824 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4825 | Token Next = NextToken(); |
| 4826 | switch (Next.getKind()) { |
| 4827 | case tok::kw_short: |
| 4828 | case tok::kw_long: |
| 4829 | case tok::kw_signed: |
| 4830 | case tok::kw_unsigned: |
| 4831 | case tok::kw_void: |
| 4832 | case tok::kw_char: |
| 4833 | case tok::kw_int: |
| 4834 | case tok::kw_float: |
| 4835 | case tok::kw_double: |
| 4836 | case tok::kw_bool: |
| 4837 | case tok::kw___pixel: |
| 4838 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4839 | return true; |
| 4840 | case tok::identifier: |
| 4841 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4842 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4843 | return true; |
| 4844 | } |
| 4845 | break; |
| 4846 | default: |
| 4847 | break; |
| 4848 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4849 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4850 | DS.isTypeAltiVecVector()) { |
| 4851 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4852 | return true; |
| 4853 | } |
| 4854 | return false; |
| 4855 | } |